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

后端 未结 7 1565
轻奢々
轻奢々 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:14

    An array cannot have zero size.

    ISO 9899:2011 6.7.6.2:

    If the expression is a constant expression, it shall have a value greater than zero.

    The above text is true both for a plain array (paragraph 1). For a VLA (variable length array), the behavior is undefined if the expression's value is less than or equal to zero (paragraph 5). This is normative text in the C standard. A compiler is not allowed to implement it differently.

    gcc -std=c99 -pedantic gives a warning for the non-VLA case.

提交回复
热议问题