我如何获得成员的结构多少? [英] How do I get the number of members in a structure?
问题描述
我要计算一个结构成员的数量。
例如:
I want to count the number of members in a structure. For example:
typedef struct
{
char MrChar;
int MrInt;
long MrLong;
} Bg_Typedef;
Bg_Typedef FooStr;
我创建了一个函数原型应该在结构返回成员人数
I create a function prototype that should return number of members in the structure
int NumberOfMem(Bg_Typedef *psStructure);
=> NumberOfMem(安培; FooStr)应该返回3
=> NumberOfMem(&FooStr) should return 3
推荐答案
有没有办法做到这一点是内置到C语言AFAIK。如果你想做到这一点,你需要记住的成员或硬code中的数量的多少作为函数的返回值。 C能告诉你在你的结构的字节大小而不是它们所包含的成员数。另外,您可以使用结构的成员函数返回成员的辛勤codeD编号。
There is no way to do this that is inbuilt into the C language AFAIK. If you want to do this you would need to remember the number of members or hard code the number as return value of your function. C can tell you the size in bytes of your structs but not the number of members they contain. Alternatively you could use a member function of your struct to return the hard coded number of members.
这篇关于我如何获得成员的结构多少?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!