C语言 声明没有声明任何东西:警告?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21610637/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 10:44:38  来源:igfitidea点击:

declaration does not declare anything : warning?

cstructwarnings

提问by kevin gomes

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

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

This code shows a warning:-

此代码显示警告:-

warning : declaration does not declare anything (enabled by default)

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

Where as the following code shows no warning

以下代码显示没有警告的地方

#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?

为什么“警告”仅显示在第一个代码中?

采纳答案by juan.facorro

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

编译器显示警告的原因是因为它没有看到address您为emp结构定义的类型变量的名称,即使您确实address在下一行声明了一些内容,但我猜编译器不够聪明弄清楚这一点。

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;
};

But not this:

但不是这个:

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

Or this:

或这个:

struct address {};

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

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 emp {
  struct phone {};
  struct name {};
};

回答by rullof

The syntax of a structure definition is:

结构定义的语法是:

struct identifier {
    type member_name;

    // ...

};

If you add an identifier just after the closing curly brace, you're declaring a variable with that defined struct.

如果您在右花括号之后添加一个标识符,那么您就是在声明一个具有该定义结构的变量。

In your first example the compiler consider the addressstruct as member type. it's like if you writes:

在您的第一个示例中,编译器将address结构视为成员类型。就像你这样写:

struct identifier {

    type ; // No member name is specified
    type a1;

    // ...

}

But in the second example you specified the member name:

但在第二个示例中,您指定了成员名称:

struct identifier {

    type a1; // Member name specified

    // ...

}

And here is an example of the warning: http://ideone.com/KrnYiE.

这是警告的示例:http: //ideone.com/KrnYiE

回答by Antti Haapala

The reason the warning is displayed is that the first excerpt is not proper C - it has a constraint violation that a standards-compliant C compiler mustproduce a diagnostisc message for. It violates the C11 6.7.2.1p2:

显示警告的原因是第一个摘录不是正确的 C - 它违反了约束,符合标准的 C 编译器必须为其生成诊断消息。它违反了C11 6.7.2.1p2

Constraints

  1. A struct-declarationthat does not declare an anonymous structure or anonymous union shall contain a struct-declarator-list.

约束

  1. 一个结构声明不声明匿名结构或匿名联合应包含结构说明符列表

Meaning that it is OK to write

意思是可以写

struct foo {
    struct {
          int a;
    };
};

since the inner structdeclares an anonymousstructure, i.e. it is not named.

因为内部struct声明了一个匿名结构,即它没有命名。

But in your example the struct addresshas a name - address- and therefore it musthave a declarator list after the closing brace - declarator list being for example a1as in your example, or more complex foo, *bar, **baz[23][45].

但是在您的示例中,它struct address有一个名称 -address因此它必须在右大括号之后有一个声明符列表 - 例如a1在您的示例中,或者更复杂的声明符列表foo, *bar, **baz[23][45]