What does “char (*a)[12]” mean?

為{幸葍}努か 提交于 2019-11-30 18:32:46

问题


Is this from the C standard?


回答1:


Because declarations in C follow the operator precedence rules (ie array subscription is evaluated before indirection), you'll need parens to declare pointers to array types.

In many use cases, there's not really any practical benefit over using a plain char *, except that it's a way to enforce the array size, especially when used as a function parameter:

void foo(char bar[42]);

is equivalent to

void foo(char *bar);

and accepts any char *, whereas

void foo(char (*bar)[42]);

will only accept pointers to arrays of size 42.

As accessing the elements of bar in the latter case is cumbersome, it might be a good idea to immediately define an equivalent char * in the function body

char *baz = *bar;

so that you can use direct subscription baz[13] instead of (*bar)[13].




回答2:


If you're confused by a C declaration, you can use the cdecl program to explain it:

~$ cdecl
Type `help' or `?' for help
cdecl> explain char (*a)[12];
declare a as pointer to array 12 of char



回答3:


A pointer to an array of 12 characters.




回答4:


a is a pointer pointing to an array of 12 characters.



来源:https://stackoverflow.com/questions/3011972/what-does-char-a12-mean

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