现在的位置: 首页 > 综合 > 正文

C语言_空结构体的大小

2012年05月27日 ⁄ 综合 ⁄ 共 306字 ⁄ 字号 评论关闭

定义一个空的结构体,那么,这个结构体占多大空间呢?

在C中,空结构的大小为0。

在C++中,空结构的大小则为1。

#include <stdio.h>


struct A
{
}aa;


int main(int argc, char *argv[])
{
        printf("%d\n", sizeof(struct A));
        printf("%d\n", sizeof(aa));
        return 0;
}

test@test:~/tmp/cbase$ gcc struct.c 
test@test:~/tmp/cbase$ ./a.out 
0
0
test@test:~/tmp/cbase$ g++ struct.c 
test@test:~/tmp/cbase$ ./a.out 
1
1

抱歉!评论已关闭.