简体   繁体   中英

struct declaration

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

First way:

struct x {};

Second way:

struct _x {} x;

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM