When I compiled the following code with gcc -Wall -pedantic -ansi -std=c89
, it compiled successfully without giving an error at the pointer assignment. Note that I convert from int (*)[4]
to int (*)[]
.
int arr[4];
int (*p_arr)[] = &arr;
Assuming that there is some reason for allowing this (incompatible?) assignment, when I try to use it, the compiler gives incomplete type error error: invalid application of ‘sizeof’ to incomplete type ‘int[]’
.
(void) sizeof(*p_arr);
This error makes me think what is the use of allowing the previous pointer assignment p_arr = &arr
? Is this assignment allowed as per the standard?
I have used incomplete struct/union type (usually for forward declaration) and also come across the error incomplete array element type
. But this incomplete array type
is new to me. Is it possible in C standard and has a use case?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…