🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c
Article #3931

Initialization of 'auto' char array

#3931
From: Thomas Wolf
Date: Wed, 06 Sep 1995 10:11
100 lines
3740 bytes
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: wolf@di.epfl.ch               | Phone: (++41 21)693 42 37
---------------------------------------------------------------------- 
Technical Details
Message-ID: <1995Sep6.111857@lglsun.epfl.ch>
Path: arc1-ca.pugleaf.net!dinews.epfl.ch!lglsun!wolf

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!dinews.epfl.ch!lglsun!wolf
From: wolf@lglsun.epfl.ch (Thomas Wolf)
Newsgroups: comp.std.c
Subject: Initialization of 'auto' char array
Followup-To: comp.std.c
Date: 6 Sep 1995 10:11:44 GMT
Organization: Ecole Polytechnique Federale de Lausanne
Distribution: world
Message-ID: <1995Sep6.111857@lglsun.epfl.ch>
NNTP-Posting-Host: lglsun2.epfl.ch
Mime-Version: 1.0
Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: 8bit
Keywords: Initialization, Character String Literal