Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Initialization of 'auto' char array

2 views
Skip to first unread message

Larry Jones

unread,
Sep 6, 1995, 3:00:00 AM9/6/95
to
In article <1995Sep...@lglsun.epfl.ch>, wo...@lglsun.epfl.ch (Thomas Wolf) writes:
> "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?"

Yes. The wording in the standard has been (or will be) corrected by
a Technical Corrigendum to make this clear.
----
Larry Jones, SDRC, 2000 Eastman Dr., Milford, OH 45150-2789 513-576-2070
larry...@sdrc.com
Oh, now don't YOU start on me. -- Calvin

Mark Brader

unread,
Sep 6, 1995, 3:00:00 AM9/6/95
to
Thomas Wolf (wo...@lglsun.epfl.ch) asks:

> > 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?

Larry Jones (scj...@thor.sdrc.com) answers:

> Yes. The wording in the standard has been (or will be) corrected by
> a Technical Corrigendum to make this clear.

It's "will be"; 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.
--
Mark Brader "Things are getting too standard around here.
m...@sq.com Time to innovate!"
SoftQuad Inc., Toronto -- Ian Darwin and David Keldsen

My text in this article is in the public domain.

Thomas Wolf

unread,
Sep 6, 1995, 3:00:00 AM9/6/95
to
This post turned out to be longer than intended, so I decided to put an
abstract at the beginning. The question is:

"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?"

This question came up recently in de.comp.lang.c: Given

void f (void)
{
char s[80] = "";

/* ... */
}

must all 80 characters of the array be initialized to '\0' or not?

I'm not sure about this because the ANSI/ISO standard says:

Section 6.5.7, "Initializers":

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 mine)

If I understand all that correctly, this means 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 this 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 character 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)?

Example number 7 on page 74 also is no help: it does state that

char s[] = "abc", t[3] = "abc";

is equal to

char s[] = {'a', 'b', 'c', '\0'},
t[] = {'a', 'b', 'c'};

but doesn't clarify the above problem case where we have a fixed-size array
of automatic storage duration that is initialized by a character string
literal shorter than the array.

Thanks for clarifying this.

Thomas

----------------------------------------------------------------------
Swiss Federal Institute of Technology | Thomas Wolf
Software Engineering Laboratory | EPFL-DI-LGL
Thomas Wolf (TW) | CH-1015 Lausanne (Suisse)
E-Mail: wo...@di.epfl.ch | Phone: (++41 21)693 42 37
----------------------------------------------------------------------

0 new messages