Tuesday, July 28, 2009

Creating an array of pointers to a struct in C?

Another "answerer" correctly explained that you


could declare an array of pointers to a struct as:





struct foo *p[100];





but it might be useful for you to know about a


utility that has been floating around the internet


for at least a couple of decades which tries to


"translate" between a human readable syntax and


C/C++ syntax for data declarations and casts.


The utility is "cdecl", and can easily be found


with google.





As an example, on this problem:





$ cdecl


cdecl%26gt; declare p as array 100 of pointer to struct foo


struct foo *p[100]





What's interesting is that it can reverse the translation:


cdecl


cdecl%26gt; explain struct foo *p[100]


declare p as array 100 of pointer to struct foo





It's not so exciting for something simple like this example,


but if you run across a declaration like:





int *const*(**f)(int*,int*);





you can feed it to cdecl to get at least *some* idea of


what it means:





cdecl%26gt; explain int *const*(**f)(int*)


declare f as pointer to pointer to function (pointer to int) returning pointer to const pointer to int





Similarly, it will generate "cast" syntax, ie:





cdecl%26gt; cast p into pointer to pointer to function returning pointer to int


(int *(**)())p

Creating an array of pointers to a struct in C?
if you mean declaration you can write in two ways


1) 'struct name


{.....}*p[100];' is to declare a global variable **p


with memory allocated.


another format is 'struct name *p[100];' in local or global scope. i think you are a begginer to c any way go go go.


I am ragesh. i am doing my second year diploma in computer engineering. For friendship and your doubts, email me in


rageshctech@yahoo.com


No comments:

Post a Comment