Reputation: 51
How can I initialize pointer to an array in C correctly
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?
Upvotes: 4
Views: 232
Reputation: 19443
int **data;
int a[10];
data = &a;
you can define data as other suggested: int (*date)[10];
but i believe that using it as a double pointer will make more seance the day you wish to change the size of that array from 10 to anything else !
Upvotes: -1
Reputation: 399949
I believe your parenthesis are wrong. You need:
int (*data)[10];
Note that you can use cdecl.org to get help with these things.
For your original code it says:
declare data as array 10 of pointer to int
For mine it says:
declare data as pointer to array 10 of int
Upvotes: 2
Reputation: 754450
Declare a pointer to an array correctly:
int (*data)[10];
Assign a pointer to an array to it:
int a[10];
data = &a;
Upvotes: 6
Reputation: 409356
The variable data
is an array of pointer, and you try to assign to it a single pointer. If you want to declare data
as a pointer to an array you have to re-arrange the parentheses:
int (*data)[10];
I recommend you read about the clockwise/spiral rule.
Upvotes: 1