Simple Array Question

Started by
10 comments, last by Krak 20 years, 6 months ago
quote:Original post by hplus0603
Brother Bob: the problem is that you index your array of two items using indices 1 and 2. You should use indices 0 and 1.

What I''m trying to show with the code is that writing outside the array can cause surprising effects. Writing to foo2[1] also was a bit redundant, maybe I should have written to foo2[2] only (apart from foo1) to show the effect.
Advertisement
quote:Original post by hplus0603
Last: this construct will NOT initialize everything to 0, only the first element:

  int myArray[ 99 ] = { 0 };  
The C++ standard says:
quote:C++ standard, 8.5.1/7
If there are fewer initializers in the list than there are members in the aggregate, then each member not explicitly initialized shall be default-initialized (8.5). [Example:
struct S { int a; char* b; int c; };
S ss = { 1, "asdf" };
initializes ss.a with 1, ss.b with "asdf", and ss.c with the value of an expression of the form int(), that is, 0. ]
I assume that this is the same for C, but I can''t be certain.

This topic is closed to new replies.

Advertisement