gpt4 book ai didi

c - GCC 关闭 "warning: declaration does not declare anything"

转载 作者:太空宇宙 更新时间:2023-11-04 03:18:10 27 4
gpt4 key购买 nike

是否有一个标志可以传递给 gcc 以禁用此警告?我知道它的作用,但这对我的程序来说无关紧要。

编辑:我只想禁用警告,保持代码不变。编译以下代码会生成警告:

struct post{
unsigned int isImg : 1;
struct img{
char *name;
char *url;
int likes;
};

unsigned int isTxt : 1;
struct text{
char*text;
int likes;
};
union Data{
struct img Img;
struct text Txt;
} data;
};

我使用的gcc版本是5.4.0

最佳答案

看着:

struct post{
unsigned int isImg : 1;
struct img
{
char *name;
char *url;
int likes;
};

unsigned int isTxt : 1;

struct text
{
char*text;
int likes;
};

union Data
{
struct img Img;
struct text Txt;
} data;
};

我看到了一些问题:

  1. 第一个32位位图的定义,只定义了一位
  2. 'img' 结构是 12 字节 + 4 字节填充符
  3. 第二个32位位图的定义,只定义了一位
  4. “文本”结构是 8 字节 + 8 字节填充符
  5. union ,由两个先前的结构类型组成
  6. 全部用封闭结构定义

数据声明的组织非常糟糕。

建议:

struct img  //32bits
{
char *name;
char *url;
int likes;
};

struct text //32bits
{
char *text;
int likes;
};

union Data // 32bits
{
struct img Img;
struct text Text;
};


struct post //5*32bits
{
unsigned int isImg : 1;
struct img image;
unsigned int isTxt : 1;
struct text Text;
union Data data;
};

即使使用上述(编译时没有错误/警告)仍然存在位图排序是实现定义的问题,因此如果不进行测试,您不知道那些定义的位是 MSB 还是 LSB位图。

关于c - GCC 关闭 "warning: declaration does not declare anything",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49351532/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com