What is the size of an empty struct in C?

前端 未结 4 1956
没有蜡笔的小新
没有蜡笔的小新 2020-11-30 06:07

According to me, it is zero but there seems to be bit confusion here

I have tested it with gcc compiler and it gives me zero as output. I know that in C++, size of an

4条回答
  •  日久生厌
    2020-11-30 06:29

    on VC 8 It gives error if we try to get the sizeof empty struct, on the other way round on linux with gcc it gives size 1 because it uses gcc extention instead of c language specification which says this is undefined behaviour.

    struct node
    {
    // empty struct.
    };
    
    int main()
    {
    printf("%d", sizeof(struct node));
    return 0;
    }
    

    on windows vc 2005 It gives compilation error on linux with gcc it gives size 1 because gcc extension http://gcc.gnu.org/onlinedocs/gcc-4.1.2/gcc/Empty-Structures.html#Empty-Structures (As Pointed out by Michael Burr)

提交回复
热议问题