Sunday, June 1, 2008

Ways of handling complex declarations in C


Question: How can you declare an array of N pointers to functions returning pointers to functions returning pointers to characters in C?


Answer: Confused? It's not that difficult. There are probably three ways of doing it:-

Declare all in one go - if you've really good understanding of the concepts of function pointers and if you're comfortable with complex declaration, you'll come up with this declaration:-

char *(*(*arr[N])())();


Building the declaration - this is an easier way of approaching complex declarations, but it takes little more steps than the previous one. Let's do it for our case:-

typedef char *pc; /*... pointer to char...*/

typedef pc fpc(); /*...fun returning pointer to char...*/

typedef fpc *pfpc(); /*... pointer to the above decl...*/

typedef pfpc fpfpc(); /*...fun returning the above...*/

typedef fpfpc *pfpfpc;/*... pointer to the above decl...*/

pfpfpc arr[N]; /*... this is the required declaration ...*/


Using 'cdel' program - this is a special program, which returns C declaration of valid English sentences and also vice-versa. You just need to enter the declaration requirement in plain English, like:-


cdel> declare arr as array of pointer to function returing pointer to function returning pointer to char




Share/Save/Bookmark


No comments: