零元素数组 [英] Array of zero element

查看:73
本文介绍了零元素数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我看到一些代码是我们有零元素的数组(大多数是结构内的



喜欢,


typedef struct foo {

int data [0]

} foo;


只是好奇使用这种代码是什么?以及如何使用struct foo的成员

变量'data''?


谢谢,

Nilesh

Hi,

I have seen some code were we have array with zero elements (mostly
within structure)

like,

typedef struct foo {
int data[0]
}foo;

Just curious whats the use of this kind of code ? And how to use member
variable ''data'' of struct foo ?

Thanks,
Nilesh

推荐答案

ni *** ********@gmail.com 说:




我有看到一些代码是我们有零元素的数组(大多数是结构内的



喜欢,


typedef struct foo {

int data [0]

} foo;


只是好奇使用这种代码是什么?
Hi,

I have seen some code were we have array with zero elements (mostly
within structure)

like,

typedef struct foo {
int data[0]
}foo;

Just curious whats the use of this kind of code ?



它有助于生成诊断消息,例如:


警告:ANSI C禁止零大小的数组`data''

警告:结构或联合结尾没有分号

It''s good for generating diagnostic messages, such as:

warning: ANSI C forbids zero-size array `data''
warning: no semicolon at end of struct or union


如何使用会员

struct foo的变量''data''?
And how to use member
variable ''data'' of struct foo ?



使它大于0,最后粘贴分号,实例化,

然后使用成员运算符。访问会员对象。


-

Richard Heathfield

Usenet是一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk

电子邮件:rjh在上述域名, - www。

Make it a big bigger than 0, stick a semicolon on the end, instantiate it,
and then use the member operator . to access the member object.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.




Richard Heathfield写道:

Richard Heathfield wrote:
ni *********** @ gmail.com 说:




我见过一些代码,我们有数组为零元素(大多数是结构内的



喜欢,


typedef struct foo {

int data [0]

} foo;


只是好奇使用这种代码是什么?
Hi,

I have seen some code were we have array with zero elements (mostly
within structure)

like,

typedef struct foo {
int data[0]
}foo;

Just curious whats the use of this kind of code ?



它有助于生成诊断消息,例如:


警告:ANSI C禁止零大小数组`data''

警告:结构或联合结束时没有分号


It''s good for generating diagnostic messages, such as:

warning: ANSI C forbids zero-size array `data''
warning: no semicolon at end of struct or union



好​​的,我用分号和编译使用gcc,它不是产生警告,

Ok, I put semicolon and compiling using gcc, its not producing warning,


>
>

如何使用成员

struct foo的变量''data''?
And how to use member
variable ''data'' of struct foo ?



使它大于0,最后粘贴分号,实例化,

然后使用成员运算符。访问会员对象。


-

Richard Heathfield

Usenet是一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk

电子邮件:rjh在上述域名, - www。


Make it a big bigger than 0, stick a semicolon on the end, instantiate it,
and then use the member operator . to access the member object.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.



ni * **********@gmail.com 写道:




我看到一些代码是我们有零元素的数组(大多数结构中是




喜欢,


typedef struct foo {

int data [0]

} foo;


只是好奇什么是使用这种代码?以及如何使用成员

struct foo的变量''data''


谢谢,

Nilesh
Hi,

I have seen some code were we have array with zero elements (mostly
within structure)

like,

typedef struct foo {
int data[0]
}foo;

Just curious whats the use of this kind of code ? And how to use member
variable ''data'' of struct foo ?

Thanks,
Nilesh



零长度数组不是标准C,而是GCC扩展。在

书中GCC - 完整参考,据说:

" GNU C允许声明零长度数组以方便
创建

可变长度结构。这只有在零长度

数组是结构的最后一个
成员时才有意义。数组的大小可以简单地指定

分配

所需的空间量。以下程序演示了

技术:

/ * zarray.c * /

#include< stdio.h>

typedef struct {

int size;

char string [0];

} vlen;

int main(int argc,char * argv [])

{

int i;

int count = 22;

char letter =''a'';

vlen * line =(vlen *)malloc(sizeof(vlen)+ count);

line-> ; size = count;

for(i = 0; i< count; i ++)

line-> string [i] = letter ++;

printf(" sizeof(vlen)=%d \ n",sizeof(vlen));

for(i = 0; i< line-> size; i ++)

printf("%c",line-> string [i]);

printf(" \ n");

返回(0);

}

"

Array of zero length is NOT standard C, but a GCC extension. In the
book "GCC - The Complete Reference", it is said that:
"GNU C allows the declaration of arrays of zero length to facilitate
the creation of
variable-length structures. This only makes sense if the zero-length
array is the last
member of a struct. The size of the array can be specified by simply
being allocated
the amount of space necessary. The following program demonstrates the
technique:
/* zarray.c */
#include <stdio.h>
typedef struct {
int size;
char string[0];
} vlen;
int main(int argc,char *argv[])
{
int i;
int count = 22;
char letter = ''a'';
vlen *line = (vlen *)malloc(sizeof(vlen) + count);
line->size = count;
for(i=0; i<count; i++)
line->string[i] = letter++;
printf("sizeof(vlen)=%d\n",sizeof(vlen));
for(i=0; i<line->size; i++)
printf("%c ",line->string[i]);
printf("\n");
return(0);
}
"


这篇关于零元素数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆