在C结构数组“的sizeof”的结果呢? [英] Result of 'sizeof' on array of structs in C?
本文介绍了在C结构数组“的sizeof”的结果呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在C,我有这样定义结构的数组:
结构ð
{
字符*一个;
字符* B;
字符* C;
};静态结构D上[] = {
{
1A,
1b时,
1C
},
{
2A
2B
2C
}
};
我想确定数组中元素的个数,但的sizeof(A)
返回不正确的结果:48,而不是2.我做错了什么,或者是的sizeof
干脆不可靠吗?如果它的事项我与 GCC编译 4.4。
解决方案
sizeof的A / sizeof的一个[0];
这是一个编译时间常数,所以你可以使用它,例如,创建另一个数组:
的#defineñsizeof的A / sizeof的一个[0]
INT N_A [N];
In C, I have an array of structs defined like:
struct D
{
char *a;
char *b;
char *c;
};
static struct D a[] = {
{
"1a",
"1b",
"1c"
},
{
"2a",
"2b",
"2c"
}
};
I would like to determine the number of elements in the array, but sizeof(a)
returns an incorrect result: 48, not 2. Am I doing something wrong, or is sizeof
simply unreliable here? If it matters I'm compiling with GCC 4.4.
解决方案
sizeof a / sizeof a[0];
This is a compile-time constant, so you can use it to, for example, create another array:
#define N sizeof a / sizeof a[0]
int n_a[N];
这篇关于在C结构数组“的sizeof”的结果呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文