Is this a correct way to define array of pointers to array in C programming language?
int* ptr[2];
int n1[5] = { 2,3,4,5,6 };
int n2[5] = { 2,3,4,5,6 };
ptr[0] = &n1;
ptr[1] = &n2;
I am getting errors like:
epi_2.c:20:12: warning: incompatible pointer types assigning to 'int *' from 'int (*)[5]' [-Wincompatible-pointer-types]
ptr[1] = &n2;
You have to write
Array designators used in expressions with rare exceptions are converted to pointers to their first elements.
That is in the above statements expressions
n1andn2have the typeint *- the type of the left side expressions.As for these statements
then the right side expressions have the type
int ( * )[5]that is not compatible with the typeint *of the left side expressions. So the compiler issues messages.Otherwise you need to declare the array of pointers like
Here is a demonstration program.
The program output is
And here is another demonstration program.
The program output is the same as shown above