【发布时间】:2015-10-30 21:27:59
【问题描述】:
在文件范围内,以下是类型声明还是未命名变量?
struct student_s {
char* name;
int age;
double height;
struct student_s* next;
};
如果是类型定义,那么与:
typedef struct student_s {
char* name;
int age;
double height;
struct student_s* next;
};
?
(背景:请参阅我在Changing a variable from global to local - C 的回答,我相信第一个会引入一个未命名的变量,然后编译器会对其进行优化。)
注意:该问题已被标记为可能与 In what scope is a struct member identifier put? 重复,但我相信我不是在问关于成员范围的问题,而是关于声明实际创建的内容。然而。 Difference between 'struct' and 'typedef struct' in C++? 的答案请解释我的问题。
【问题讨论】:
-
为什么你认为它会定义一个未命名的变量,而不是没有变量?您是否期待第二个 sn-p 的未命名类型别名?
-
您也可以查看this thread 了解更多信息
-
您发布的链接指向一个不属于您的答案。你想要这个:stackoverflow.com/a/31874396/2793118 前者是一个类型定义,后者是无效的,因为
typedef没有名称参数。 -
区别:第一个是有效的C,第二个不是。