What happens if I define a 0-size array in C/C++?

后端 未结 7 1512
轻奢々
轻奢々 2020-11-22 03:47

Just curious, what actually happens if I define a zero-length array int array[0]; in code? GCC doesn\'t complain at all.

Sample Program

7条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-22 04:11

    I'll add that there is a whole page of the online documentation of gcc on this argument.

    Some quotes:

    Zero-length arrays are allowed in GNU C.

    In ISO C90, you would have to give contents a length of 1

    and

    GCC versions before 3.0 allowed zero-length arrays to be statically initialized, as if they were flexible arrays. In addition to those cases that were useful, it also allowed initializations in situations that would corrupt later data

    so you could

    int arr[0] = { 1 };
    

    and boom :-)

提交回复
热议问题