Inconsistent gcc diagnostic for string initialization

While:

 char a[5] = {'h','e','l','l','o','\0'};

is invalid.

(C11, 6.7.9p2) “No initializer shall attempt to provide a value for an object not contained within the entity being initialized.”

This:

char b[5] = "hello";

is explicitly allowed by C (emphasis mine):

(C11, 6.7.9p14) “An array of character type may be initialized by a character string literal or UTF−8 string literal, optionally enclosed in braces. Successive bytes of the string literal (including the terminating null character if there is room or if the array is of unknown size) initialize the elements of the array.”

But

 char b[5] = "hello!";

is invalid.

Leave a Comment