下面的代码是关于 C 中嵌套结构的在线 资源 的副本。 该程序定义了2个结构体,“组织”和“员工”,然后在主函数中将创建“组织”对象。
注释说程序会出错。事实上,我在网上发现编译器按预期打印了一个错误,说“警告:声明没有声明任何内容”,但仍然显示输出。
但是 GCC 编译器会解释这些行并定义另一个名为“Employee”的非嵌套结构,即使它位于结构“Organization”的定义内。
所以,我想知道其背后的逻辑。预先感谢您!
#include <stdio.h>
// Declaration of the outer
// structure
struct Organisation
{
char organisation_name[20];
char org_number[20];
// Declaration of the employee
// structure
struct Employee
{
int employee_id;
char name[20];
int salary;
// **This line will cause error because **
// datatype struct Employee is present, but Structure variable is missing.
};
};
//continues
结果如果我用命令
gcc test.c -ansi -pedantic
编译程序,它会显示warning: ISO C90 doesn't support unnamed structs/unions [-Wpedantic] };
,这意味着程序可以无错误地执行,但它可能违反某些标准,一些编译器不允许,而没有选项的gcc允许?
但这可能违反某些标准
取决于你如何看待它。标准没有禁止,但也没有要求。