C pointer to array/array of pointers disambiguation

前端 未结 13 1341
我寻月下人不归
我寻月下人不归 2020-11-21 05:19

What is the difference between the following declarations:

int* arr1[8];
int (*arr2)[8];
int *(arr3[8]);

What is the general rule for under

13条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-21 06:07

    I don't know if it has an official name, but I call it the Right-Left Thingy(TM).

    Start at the variable, then go right, and left, and right...and so on.

    int* arr1[8];
    

    arr1 is an array of 8 pointers to integers.

    int (*arr2)[8];
    

    arr2 is a pointer (the parenthesis block the right-left) to an array of 8 integers.

    int *(arr3[8]);
    

    arr3 is an array of 8 pointers to integers.

    This should help you out with complex declarations.

提交回复
热议问题