3
int main()
{
    int b[2][3]={{1,2},{3,4,5}};
    cout << b[0][2] << endl;
}

And the result in both visual and g++ is 0! Why? I thought it must be another number! For example, when we define int a[5] then we say cout << a[3]; without setting a[3], it will be something like, 0123984283 which means the last value of this cell in the RAM.

But here, What's the reason of 0?

Milad R
  • 1,854
  • 9
  • 25
  • 36

2 Answers2

5

If it is Partial Initialization, yes rest of the elements are guaranteed to be 0.

Here is a good read for Standerdese Fans and ones with eye for detail:

C and C++ : Partial initialization of automatic structure

Community
  • 1
  • 1
Alok Save
  • 202,538
  • 53
  • 430
  • 533
  • What do you mean by Partial Initialization? – Milad R Jun 17 '12 at 09:42
  • @MiladR: Please read the linked answer, it explains in detail, why and how. – Alok Save Jun 17 '12 at 09:44
  • *Partial initialization* is not a term of C++, it is the term that someone used to ask a question. The term (for what is referred in the link as partial/full initialization) is **aggregate initialization**, and there is no specific term for *aggregate initialization* where not all of the fields are mentioned in the initializer list. – David Rodríguez - dribeas Jun 17 '12 at 12:20
1

This answer has a more complete explanation with references to the documentation:

https://stackoverflow.com/a/629063/475523

Community
  • 1
  • 1
src
  • 541
  • 2
  • 6