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

后端 未结 4 1768
失恋的感觉
失恋的感觉 2021-01-06 11:39

Is this from the C standard?

4条回答
  •  清酒与你
    2021-01-06 12:26

    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].

提交回复
热议问题