本文介绍了sizeof的结构成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎样才能获得成员在结构尺寸用C?

 一个结构
{
        焦炭ARR [64];
};

我需要类似的东西:

的sizeof(A :: ARR)

感谢


解决方案

 的sizeof(((结​​构A *)0) - > ARR);

简单地说,投一个空指针类型结构A * ,但因为中的sizeof 操作数是没有评估,这是合法的,并让你获得结构成员的大小,而无需创建结构的实例。

基本上,我们是它的一个实例存在于地址0可用于pretending偏移和的sizeof 决心。

要进一步阐述,请阅读这篇文章:

http://www.embedded.com/design/prototyping-and-development/4024941/Learn-a-new-trick-with-the-offsetof--macro

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

解决方案
sizeof(((struct A*)0)->arr);

Briefly, cast a null pointer to a type of struct A*, but since the operand of sizeof 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

这篇关于sizeof的结构成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 21:33