声明没有声明任何东西:警告? [英] declaration does not declare anything : warning?

查看:67
本文介绍了声明没有声明任何东西:警告?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>

int main()
{
    struct emp
    {
        struct address
        {
              int a;
        };
        struct address a1;
    };
}

此代码显示警告:-

警告:声明没有声明任何东西(默认启用)

以下代码不显示警告

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>

int main()
{
    struct emp
    {
        struct address
        {
             int a;
        }a1;
    };
}   

为什么只在第一个代码中显示警告"?

Why 'warning' is displayed in the first code only?

推荐答案

编译器显示警告的原因是因为它没有看到您定义的 address 类型的变量的名称对于 emp 结构,即使您在下一行 do 使用 address 声明了一些东西,但我猜编译器不够聪明,无法计算那个出来.

The reason why the compiler is showing the warning is because it doesn't see a name for the variable of type address you defined for the emp struct, even though you do declare something using address on the next line, but I guess the compiler is not smart enough to figure that out.

正如你所展示的,这会产生一个警告:

As you showed, this produces a warning:

struct emp {
  struct address {}; // This statement doesn't declare any variable for the emp struct.
  struct address a1;
};

但不是这个:

struct emp {
  struct address {} a1; // This statement defines the address struct and the a1 variable.
};

或者这个:

struct address {};

struct emp {
  struct address a1; //the only statement declare a variable of type struct address
};

struct emp {} 不显示任何警告,因为此语句不在结构定义块内.如果您确实将其放入其中之一,那么编译器也会为此显示警告.以下将显示两个警告:

The struct emp {} doesn't show any warnings since this statement is not inside a struct defintion block. If you did put it inside one of those then the compiler will show a warning for that as well. The following will show two warnings:

struct emp {
  struct phone {};
  struct name {};
};

这篇关于声明没有声明任何东西:警告?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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