char test[0] cannot pass through the compilation. If change to char test[1] then the result will 4, 1 respectively.
zhouzhaohan() ( ) 信誉:100 C中,在struct中定义的char *test和char test[0]有什么区别呢? char *test定义了一根指针,占用空间。 char test[0]只定义了一个符号test,并不实际占用空间,是对c语言的一个扩展。 至于sizeof是多大,哪要看你的结构如何定义,结构中的成员顺序如何。 GCC中有这个扩展,richard stallman 是这样解释这种做法的原因和使用方法的: Arrays of Length ZeroZero-length arrays are allowed in GNU C. They are very useful as the last element of a structure which is really a header for a variable-length object: struct line { int length; char contents[0]; };{ struct line *thisline = (struct line *) malloc (sizeof (struct line) + this_length); thisline->length = this_length; }In standard C, you would have to give contents a length of 1, which means either you waste space or complicate the argument to malloc.
// sizeof( array[0] ) is 4
size_t sizearr = // Count of items in array
sizeof( array ) / sizeof( array[0] );数组==>指针?不明白
sizeof(test) == 4;
char test[0];
sizeof(test) == 1;
数组名在作为参数传递的时候退化为一个纯粹的指针了
此时sizeof的时候是4,否则sizeof就是整个数组的长度的
但元素个数等于零的数组在编译(levels 2 and 4)时会有警告.vc6下sizeof()的值两个显然不同第一个时一个指针的大小4,而第二个是整个数组的大小0
char test[0];
sizeof(test)中的test也会退化成指针.如果退化的话,你用什么得到数组的大小,愿请教.
就以test[0]为例
你先说说你的见解.
再sizeof()试一下看看;
char *test定义了一根指针,占用空间。
char test[0]只定义了一个符号test,并不实际占用空间,是对c语言的一个扩展。
至于sizeof是多大,哪要看你的结构如何定义,结构中的成员顺序如何。
GCC中有这个扩展,richard stallman 是这样解释这种做法的原因和使用方法的:
Arrays of Length ZeroZero-length arrays are allowed in GNU C. They are very useful as the last element of a structure which is really a header for a variable-length object: struct line {
int length;
char contents[0];
};{
struct line *thisline = (struct line *)
malloc (sizeof (struct line) + this_length);
thisline->length = this_length;
}In standard C, you would have to give contents a length of 1, which means either you waste space or complicate the argument to malloc.