是一个结构{...};一个类型或者一个未命名的变量? [英] Is a struct {...}; a type or an unnamed variable?

查看:130
本文介绍了是一个结构{...};一个类型或者一个未命名的变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如下,在文件范围,类型声明或一个未命名的变量?

Is the following, at file scope, a type declaration or an unnamed variable?

struct student_s {
    char* name;
    int age;
    double height;
    struct student_s* next;   
};

如果它是一种类型的定义,则是用差:

If it is a type definition, then what is the difference with:

typedef struct student_s {
    char* name;
    int age;
    double height;
    struct student_s* next;   
};

(背景:见我的答案在<一个href=\"http://stackoverflow.com/questions/31874189/changing-a-variable-from-global-to-local-c/31874396#31874358\">Changing从全球到地方的变量 - C ,在这里我认为首先介绍了一位不愿透露姓名的变量,然后将其优化掉由编译器)

(Background: see my answer at Changing a variable from global to local - C, where I believe the first introduces an unnamed variable that is then optimized away by the compiler.)

注意:这个问题已被标记为在什么范围内是一个结构成员标识符放?但我相信我不是问有关成员的范围,但关于什么的声明实际创建的问题。然而。答案<一个href=\"http://stackoverflow.com/questions/612328/difference-between-struct-and-typedef-struct-in-c\">Difference在C'结构'和'typedef结构++?做解释我的问题。

Note: the question has been flagged as a possible duplicate of In what scope is a struct member identifier put? but I believe I am not asking a question about the scope of the members but about what the declarations actually create. However. answers to Difference between 'struct' and 'typedef struct' in C++? do explain my question.

推荐答案

由于每 C 标准,一个结构声明的形式如

As per the C standard, a structure declaration in the form like

struct student_s {
    char* name;
    int age;
    double height;
    struct student_s* next;   
};

键入的声明。引用 C11 ,章§6.7.2.1

is the declaration of a type. Quoting C11, chapter §6.7.2.1

在一个结构或 - 工会说明一个struct声明列表的presence声明了一个新的类型,一个翻译单元内。该结构声明列表是声明的结构或联合的成员的序列。 [...]

The presence of a struct-declaration-list in a struct-or-union-specifier declares a new type, within a translation unit. The struct-declaration-list is a sequence of declarations for the members of the structure or union. [...]

C 标准并没有强制要求创建的变量的(无论是有名或无名)为该类型。

C standard does not mandate creating a variable (either named or unnamed) for that type.

在你的第二个片段中,你实际上(的尝试的)的typedef 没有即可。但是,如果你改变你的代码片段,以形式

In your second snippet, you actually (try to) typedef to nothing. However, if you change your snippet to the form of

typedef struct {
         //.....members
} student_s ;

您将创建一个键入 student_s 的typedef 一位不愿透露姓名的结构输入),你可以在以后使用。

you'll be creating a type student_s (typedef to an unnamed structure type) which you can use later.

FWIW,我们从来没有谈到的变量是否在这里创建,反正。这是所有关于类型

FWIW, we never talked about a variable being created here, anyways. It's all about types.

这篇关于是一个结构{...};一个类型或者一个未命名的变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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