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?
Declare a pointer to an array correctly:
int (*data)[10];
Assign a pointer to an array to it:
int a[10]; data = &a;
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
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 .
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 !
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.