结构体成员的大小
如何获取 C 结构体中成员的大小?
struct A
{
char arr[64];
};
我需要这样的东西:
sizeof(A::arr)
谢谢
How can I get the size of a member in a struct in C?
struct A
{
char arr[64];
};
i need something like that:
sizeof(A::arr)
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
简而言之,将空指针转换为 struct A* 类型,但由于不计算 sizeof 的操作数,因此这是合法的,并且允许您获取结构成员的大小无需创建该结构的实例。
基本上,我们假设它的实例存在于地址 0 处,并且可用于确定偏移量和 sizeof 。
要进一步详细说明,请阅读这篇文章:
http://www.embedded.com/design/prototyping-and-development/4024941/Learn-a-new-trick-with-the-offsetof--macro
Briefly, cast a null pointer to a type of
struct A*
, but since the operand ofsizeof
is not evaluated, this is legal and allows you to get size of struct members without creating an instance of the struct.Basically, we are pretending that an instance of it exists at address 0 and can be used for offset and
sizeof
determination.To further elaborate, read this article:
http://www.embedded.com/design/prototyping-and-development/4024941/Learn-a-new-trick-with-the-offsetof--macro