The correct syntax for initializing an array pointer

How can I correctly initialize an array pointer in C

Here is my code

int (*data[10]); int a[10]; data = &a[0]; /* gives a warning "int *" cannot be assigned to entity of "int (*)[10]" */ 

How can I get rid of this warning?

+4
source share
4 answers
  • Correctly declare a pointer to an array:

     int (*data)[10]; 
  • Assign to a pointer to an array:

     int a[10]; data = &a; 
+6
source

I believe your brackets are wrong. You need:

 int (*data)[10]; 

Please note that you can use cdecl.org to get help with this.

Your source code says:

declare data as an array of 10 pointers to int

In my opinion it says:

declare data as a pointer to array 10 of int

+2
source

The data variable is an array of a pointer, and you are trying to assign one pointer to it. If you want to declare data as a pointer to an array, you need to reorder the brackets:

 int (*data)[10]; 

I recommend that you read the rule clockwise / spiral .

+1
source
 int **data; int a[10]; data = &a; 

you can define data as others: int (*date)[10];
but I believe that using it as a double pointer will make more session that day when you want to resize this array from 10 to everything else!

-1
source

All Articles