Wednesday, June 27, 2007

Even more tuff assignments

These series of assignments tell you how tough pointers can become if not understood properly.....

int **x; //this is a pointer to a pointer that ponts to an onteger..lets imagine..we have a box from where there is an arrow to an other box.Now an arrow from that arrow to a box that stores integer..Thats how it is...seems difficult....then better stay here

int (*x)[5]; //this is as discussed in the previuos posts.....pointer to an integer array of 5 elements..so there is a box now with space for 5 elements and now x points to that box...
int *x[5]..this is similar to int* x[5];

The second asignment makes life imple...thats how the compiler groups it without a parenthesis...so it is array of 5 integer pointers....there is a box now and each element inside the box points to an integer.simple...

int *compute(); ///what can this be....the * goes with the int and compute a function that returns a pointer to integer

int (*compute)(); //compute is a pointer to a function returning int.these are simple ones...but it can get tougher now.....

char *(*(compute()))(); // as we can see here ther is a function returning pointer to character ..lets break it up... char* (*(compute())) () ;so lets remove the outer function..what remains

(*(compute())) //here we can see an inner function compute .There is one extra set of parenthesis that can be removed....so it is now (*compute()).this is actually a function that returns a pointer now.what is that pointer.it is actually a function that returna pointer to char

Therefore compute if a function that returns a pointer to a function returning a pointer to char //so the definition is aline and a half...now even tougher one..take a breath


int (*(*(x[5])))[5]; //as we can see no functions here all arrays...not easy..wait..

so we have as stated (*(x[5])) points to that box of 5 elements....lets break it up even further.....
applying the & operator we get that x[5] stores the starting address of the box.

so x is an array with 5 elemsts where each element stores the address of integer arrays of length 5 each.

Such are the pointer assignments in the C programming language ..there can be even more tuff assignments though such stuffs are rarely used while programing..ppl give such assignments as commenst with or without solutions

Thanks,
Prasanna

so constructing it back......

No comments: