struct declaration

喜你入骨 提交于 2019-12-31 07:03:28

问题


What is the difference between these 2 ways of declaring a struct?

First way:

struct x {};

Second way:

struct _x {} x;

回答1:


The first defines only the type struct x. The second defines the type struct _x and defines a variable of that type named x.

Though it's probably not what you had in mind, names starting with an underscore like _x are reserved at file scope, so unless this is inside some other scope, the second has undefined behavior.




回答2:


The second way declares a variable named the type struct _x and a variable of this type named x. The first one only declares the type struct x.

The second way is essentially the same as

struct _x{};    // define a type
struct _x x;    // allocate a variable of type struct _x


来源:https://stackoverflow.com/questions/13651227/struct-declaration

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!