0

what is the correct declaration of pointer to a pointer to array of N elements.
( i want to store address of pointer to an array of n elements )
i am using the below declarions seperately , but getting errors.

int (*p)[4];
          int (*p1) (*)[4] = &p;
          int *p1 (*)[4] = &p;
          int (*p1) ( (*)[4] ) = &p;

any idea please..

3
Contributors
2
Replies
4
Views
7 Years
Discussion Span
Last Post by UncleLeroy
0

There is a command-line program called "cdecl" which comes with many compilers, that helps explain declarations. It can go in either direction.
For example, type either of these as input, the other is the output:
explain int (*p)[4]
declare p as pointer to array 4 of int

The others are syntax errors.

Normally you do not want a pointer to the entire array, because incrementing the pointer would step over the entire array.
Instead you should point to the first element of the array, so that incrementing steps by one array element.

int the_array[4];
int *p = the_array;
for (int x = 0; x < 4; x++, p++) {
printf("[%d] = %d\n", x, *p);
}

You can also make it explicit that you are pointing to an element,
but it (the compiler) generates the same machine code as above.
int *p = &(the_array[0]);

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.