Can sizeof return 0 (zero)

后端 未结 10 1558
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-28 08:14

Is it possible for the sizeof operator to ever return 0 (zero) in C or C++? If it is possible, is it correct from a standards point of view?

10条回答
  •  囚心锁ツ
    2020-11-28 08:51

    Empty structs, as isbadawi mentions. Also gcc allows arrays of 0 size:

    int a[0];
    sizeof(a);
    

    EDIT: After seeing the MSDN link, I tried the empty struct in VS2005 and sizeof did return 1. I'm not sure if that's a VS bug or if the spec is somehow flexible about that sort of thing

提交回复
热议问题