C pointer to array/array of pointers disambiguation

前端 未结 13 1677
我寻月下人不归
我寻月下人不归 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 05:47

    Use the cdecl program, as suggested by K&R.

    $ cdecl
    Type `help' or `?' for help
    cdecl> explain int* arr1[8];
    declare arr1 as array 8 of pointer to int
    cdecl> explain int (*arr2)[8]
    declare arr2 as pointer to array 8 of int
    cdecl> explain int *(arr3[8])
    declare arr3 as array 8 of pointer to int
    cdecl>
    

    It works the other way too.

    cdecl> declare x as pointer to function(void) returning pointer to float
    float *(*x)(void )
    

提交回复
热议问题