sizeof(struct name_of_struct) vs sizeof(name_of_struct) 之间的区

Difference between sizeof(struct name_of_struct) vs sizeof(name_of_struct)?

本文关键字:struct sizeof of name vs 之间      更新时间:2023-10-16
两者

之间有什么区别:

sizeof(struct name_of_struct)

sizeof(name_of_struct)

主题的返回值相同。

两者之间有什么区别吗?即使它很微妙/不重要,我也想知道。

struct name_of_struct明确地指的是struct/class标记name_of_structname_of_struct可以是变量或函数名称。

例如,在 POSIX 中,您同时拥有struct stat和一个名为 stat 的函数。当你想引用结构类型时,你需要 struct 关键字来消除歧义(+纯 C 总是需要它 - 在纯 C 中,常规标识符与结构标签位于单独的命名空间中,并且结构标签不会像它们在 C++ 中那样泄漏到常规标识符命名空间中,除非您像 typedef struct tag{ /*...*/ } tag; 中那样使用 typedef 将它们显式拖到那里(。

例:

struct  foo{ char x [256];};
void (*foo)(void);
int structsz(){ return sizeof(struct foo); } //returns 256
int ptrsz(){ return sizeof(foo); } //returns typically 8 or 4

如果这看起来令人困惑,它基本上是为了保持与 C 的向后兼容性而存在的。

当然,

sizeof name_of_struct

仅当name_of_struct是对象(而不仅仅是类型(时,才为有效表达式;写入

sizeof(struct name_of_struct)

除非name_of_struct已经定义为(完整类型(为结构(或类(,否则不会编译。 但是等等,还有更多! 如果两者都存在,那么

sizeof(struct name_of_struct) 

会让你得到类型的大小,而不是对象的大小,而两者

sizeof(name_of_struct)

(sizeof name_of_struct)

将为您提供对象的大小。

据我所知:区别在于 c 样式结构表示法与C++表示法。