简体   繁体   中英

Declaring a function pointer returning an array

For practice, I'm trying to :

Declare fubar to be a pointer to a function that takes a pointer to a char and returns a pointer to an array of 24 elements where each element is a pointer to a struct foo .

My logic is:

-fubar is a pointer to a function taking a char pointer: (*fubar)(char*)

-...returning a pointer to an array of 24 elems of where each elem is a struct foo:

(struct foo *)(*fubar)(char*)[24]

Is my logic correct?

Functions never return arrays, they may return a pointer (conventionally to the first cell of an array), since as return value or as argument an array decays to a pointer.

So, declare with a typedef the signature of your function:

typedef struct foo** funsig_t(char*);

notice that if you omit the typedef you would declare a function funsig_t of the desired signature .

then declare a pointer using that typedef:

funsig_t* fubar;

After fixing the syntax error and removing the parentheses around struct foo * ,

struct foo* (*fubar)(char*)[24]

...the one part that you got wrong is that it actually returns an array of pointers, not a pointer to an array. In order to declare a pointer to the array, you need an extra set of parentheses:

struct foo (*(*fubar)(char*))[24]

You can pretend that that the star belongs to an identifier (ie, the name of the array) inside the parentheses.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM