String Initializations in C |
This is a problem I was first confronted with in September 1995. The basic question is the following:
In the initialization of a character array with fixed size with automatic storage duration by a character string literal with fewer characters (including the terminating null character) than the array has elements, are the remaining array elements initialized as if the array had static storage duration?
Let's look at an example to make sense out of this. Given:
void f (void) { char s[80] = ""; /* ... */ }
must all 80 characters of the array be initialized to 0 or not?
The standard is not too clear about this: It says [ISO, 6.5.7]:
- 3rd Paragraph:
- If an object that has automatic storage duration is not initialized explicitly, its value is indeterminate. ...
- 8th Paragraph:
- An array of character type may be initialized by a character string literal, optionally enclosed in braces. Sucessive characters of the character 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.
- 10th Paragraph:
- Otherwise, the initializer for an object that has aggregate type shall be a brace-enclosed list of initializers for the members of the aggregate, ...
- 12th Paragraph:
- If there are fewer initializers in a brace enclosed list than there are members of an aggregate, the remainder of the aggregate shall be initialized implicitly the same as objects that have static storage duration.
(Emphasis added.)
This might be interpreted such that the initialization
char s[80]={0};
really forces the compiler to set all 80 characters of the array to 0. However, the initialization
char s[80]="";
might allow the compiler to treat it like
char s[80]; s[0] = 0;
If this were not so, then why is there the restrictive "in a brace enclosed list" in the 12th paragraph? And why doesn't it say in the 8th paragraph
... If there are fewer characters in the string literal than there are elements in the array, the remaining array elements shall be initialized as if the array had static storage duration.
Furthermore, what is supposed to happen in the case of
char s[80] = {""}
(the "optionally enclosed in braces" case)?
Note that the examples given in the standard also are no help: they only deal with arrays of indeterminate size or string literal initializers longer than the arrays.
Such is the "vagueness" of a programming language standard that I spoke of in the introduction of this page. Luckily, this point seems to have been resolved in "Technical Corrigendum 2":
In article <1995Sep6.200152.18573@sq.com> in comp.std.c, Mark Brader (msb@sq.com) answered the question as follows:
...this fix is part of TC2, which is currently up for balloting.
The proposed wording of the correction to 6.5.7 (which was 3.5.7 in ANSI Classic) makes the applicable paragraph read as follows. (Emphasis added.)
If there are fewer initializers in a brace-enclosed list than there are members of an aggregate, or fewer characters in a string literal or wide string literal used to initialize an array of known size, and elements of character or wchar_t type, the remainder of the aggregate shall be initialized implicitly the same as objects that have static storage duration.
In other words: yes, if a string literal in an initializer contains less characters than the array has elements, the remaining elements are set to 0.
Copyright © 2000 by
Thomas Wolf. All rights
reserved.
TW, Aug 01, 2000 |