我想从我自己的结构中使用sizeof(_test.header.columns)/sizeof(struct _column)
获得“列”元素数。不幸的是我总是把它作为0
,因为sizeof(_test.header.columns)
总是4
。这里是代码:sizeof报告错误的值
struct _column{
char title[40];
int length;
};
struct test_struct{
struct{
struct _column* columns;
}header;
struct{
struct _column* columns;
}details;
struct{
struct _column* columns;
}end;
};
struct test_struct _test = {
.header = {
.columns = {
{
"a",
1,
},
{
"ab",
2,
},
},
},
.details = {
.columns = {
{
"b",
2,
},
{
"bc",
3,
},
},
},
.end = {
.columns = {
{
"c",
3,
},
{
"cd",
4,
},
},
},
};
void testme(){
char buff[20];
itoa(sizeof(_test.header.columns)/sizeof(struct _column),buff,sizeof(buff));
MessageBoxA(NULL,buff,NULL,NULL);
}
请帮我解决问题,谢谢。任何帮助,将不胜感激。
您在指针上使用'sizeof',而不是数组。 –
我使用了“struct _column * columns”将列声明为_column数组的声明,如果这是错误的,那么如何纠正它? – user3407088
你在看testme()的输出吗,你的参数按MessageBoxA的正确顺序排列......我不知道它们是什么......为什么不用'snprintf(buffer,20,“%d “,number)' –