Wednesday, June 27, 2007

some intriguing assignments

Lets go thru a series of assignments here using pointers and array and see how far can we crack it

int *p; //simplke one - p is a pointer to an integer variable but does not point to anything now

int i; //straight

*p = i; //p now points to i that is the address of i is the value of p

p = &i; //this tells the story more clearly

int x[5]; //x is an array we know but it points to the first element.That is x is a ponter to x[0].so *x is the value of x[0].

x+1 // his points to X[1].so *(x+1) is the value x[1] and similarly....infact the array indices starts from 0 because the implementation is like this the n+1th elemsnt is *(x + n) ..this is x[n]..that is how it is computed

So arrays are implicit pointers.......now

p++; //will move the pointer p to the next integer variable in the memeory that is the pointer will be shifted by either 2 or 4 bytes depending upon the compiler

x++; //error this cannot happen arrays are static pointers implicitly

No comments: