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

Recommended Answers

All 2 Replies

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

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]);

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.