When is pointer to array useful?

前端 未结 9 1675
天涯浪人
天涯浪人 2020-12-30 09:18

I can declare:

int (*ap)[N];

So ap is pointer to int array of size N. Why is this ever useful? If I pass it to function, what usefu

9条回答
  •  囚心锁ツ
    2020-12-30 09:54

    It seems pretty useless to me to do a pointer to an array. In C, an array is already a pointer to a block of that data type.

    int (*ap)[N];
    int **ipp;
    

    are both the same data type (a pointer to a pointer to an Integer). The only difference there is that there is space for N integers allotted for ap.

    There's no need to pass an array by a pointer to a function, like, for the purpose of changing the contents of the array within that function because it's already a pointer. As a general rule, I'd say it's unnecessary and just creates an additional need to dereference the pointer to get at the data in the array. But, I'm sure there's a program or algorithm somewhere that could find a legitimate use for it.

提交回复
热议问题