Declaring a function pointer returning an array

假如想象 提交于 2019-12-02 14:16:17

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 (i.e., the name of the array) inside the parentheses.

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;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!