Array initialization

From: R (tinwhistle_at_poczta.onet.pl)
Date: 09/23/04


Date: Thu, 23 Sep 2004 11:26:06 +0200

K&R 2nd Edition
4.9
"If there are fewer initializers for an array than the specified size,
the others will be zero for external, static and automatic variables. It
is an error to have too many initializers. There is no way to specify
repetition of an initializer, nor to initialize an element in the middle
of an array without supplying all the preceding values as well."

1) int Arr1[5];
2) int Arr2[5] = { };
3) int Arr3[5] = {0};

My compiler is BC++ 5.5

1) Arr1 is initialized with zeros, but AFAIK it's compiler dependant,
isn't it?
2) Arr2 - this does not compile. I thought that it would initialize the
array with zeros, because there are fewer initializers for an array (in
this case none) than the specified size, the others (all) will be zero.
Does the standard disallow empty initializer list?
3) This works as intended.

I'd like to know the answers for both: C and C++.
Thanks for help!
R



Relevant Pages

  • Re: Array initialization
    ... >It is an error to have too many initializers. ... >specify repetition of an initializer, nor to initialize an element in ... >the middle of an array without supplying all the preceding values as well." ... >this case none) than the specified size, the others will be zero. ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Need a GURU! Im stuck....
    ... initialise it using ordinary array initialisation syntax.] ... struct element *elems; ... one can use designated initializers if supported. ... Still, it would be more than nice to generate rowsautomatically, for which an external tool is unfortunately needed. ...
    (comp.lang.c)
  • Re: Multi-dimension array assignments
    ... If the initializer of a subaggregate or contained union ... > matching right brace initialize the elements or members of the subaggregate ... only enough initializers from the list ... > guess is "yes" as array members need to be contiguous in memory, ...
    (comp.lang.c)
  • Re: [C] strange bug in code
    ... If you one no initializers whatsoever, ... uninitialized (garbage). ... may not want to pay the cost of initializing the array. ... also probably helps /buggy/ string processing code to crash more "politely" ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Constant arrays
    ... in Delphi from 1995 until a year or so ago (and still ... As you may know Delphi  has advanced constant data ... you've never had to use array constant initializers. ...
    (comp.lang.oberon)