Professional Documents
Culture Documents
Lecture : 02
main()
{
int x[6];
int j;
for(j = 0; j < 6; j++)
x[j] = 2 * j; //We have declared an int array of six
elements and initialized it in the loop.
}
Monday, January 18, 2021 3
Arrays
Let’s revise some of the array concepts:
Array declaration: int x[6]; or float x[6]; or double x[6];
An array is collection of cells of the same type.
The reason is the name x is not lvalue and we cannot assign any
value to it. For understanding purposes, consider x as a constant. Its
name or memory location can not be changed. This is a collective
name for six locations. We can access these locations as x[0], x[1]
up to x[5].
Monday, January 18, 2021 8
Dynamic Arrays
You would like to use an array data structure but you do not know
the size of the array at compile time.
You find out when the program executes that you need an integer
array of size n=20.
On finding it, the computer will give the address of first location to
the programmer which will be stored in y.
When we said int* y = new int[20]; the new returns the memory
address of first of the twenty locations and we store that address
into y. As y is a pointer variable so it can be used on the left-hand
side. We can write it as:
y = &x[0];
y = x;
the statement y = x is also correct. x is an array of six elements that
holds the address of the first element. But we cannot change this
address. However we can get that address and store it in some other
variable. As y is a pointer variable and lvalue so the above
operation is legal
Dynamic Arrays
We have dynamically allocated the memory for the array. This
memory, after the use, can be released so that other programs
can use it. We can use the delete keyword to release the memory.
The syntax is:
delete[ ] y;
We would not do this to the x array because we did not use new
to create it.
foo = &myvar;
myvar = 25;
foo = &myvar;
bar = myvar;
The values contained in each variable after the execution of this are
shown in the following diagram:
Reference operator (&)
baz = *foo;
This could be read as: "baz equal to value pointed to by foo", and the
statement would actually assign the value25 to baz, since foo is 1776,
and the value pointed to by 1776 (following the example above)
would be 25.
Dereference operator (*)
myvar = 25;
foo = &myvar;
myvar == 25
&myvar == 1776
foo == 1776
*foo == 25
Declaring Pointer Variables
The value of a pointer variable is an address. That is the value
refers to another memory space. The data is typically stored in this
memory space. Therefore when you declare a pointer variable, you
also specify the data type of the value to be stored in the memory
location to which the pointer variable points.
short *myshort;
long *mylong;
++mychar;
++myshort;
++mylong;
mychar, as one would expect, would contain the value 1001. But not so
obviously, myshort would contain the value 2002, and mylong would
contain 3004, even though they have each been incremented only
once.
Pointer Arithmetic
int x;
int y = 10;
const int * p = &y;
x = *p; // ok: reading p
*p = x; // error: modifying p, which is const-qualified