|
|
|
#1 |
|
Messages: n/a
Hébergeur: |
Hi All,
What is the C-standard expected result for referring the 'uninitialized element' of the partial initialized automatic array ? /*************************/ #include <stdio.h> int main(void) { int arr[2][3][4] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21 } ; printf(" arr[1][2][2] = %d \n", arr[1][2][3]); return 0; } /*************************/ Is the result defined as 0 by standard? If yes, then, why is that so? Thanks, Naresh |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
Nishu wrote:
> Hi All, > > What is the C-standard expected result for referring the > 'uninitialized element' of the partial initialized automatic array ? It is initialised to zero. > Is the result defined as 0 by standard? If yes, then, why is that so? Because the Standard says so. |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
On Wed, 28 Nov 2007 22:00:31 -0800 (PST), Nishu
<naresh.attri@gmail.com> wrote in comp.lang.c: > Hi All, > > What is the C-standard expected result for referring the > 'uninitialized element' of the partial initialized automatic array ? > > /*************************/ > #include <stdio.h> > > int main(void) > { > int arr[2][3][4] = { 0, 1, 2, 3, > 4, 5, 6, 7, > 8, 9, 10, 11, > > 12, 13, 14, 15, > 16, 17, 18, 19, > 20, 21 > } ; > > printf(" arr[1][2][2] = %d \n", arr[1][2][3]); > > return 0; > } > > /*************************/ > > Is the result defined as 0 by standard? If yes, then, why is that so? Why not? -- Jack Klein Home: http://JK-Technology.Com FAQs for comp.lang.c http://c-faq.com/ comp.lang.c++ http://www.parashift.com/c++-faq-lite/ alt.comp.lang.learn.c-c++ http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
Nishu wrote:
> Hi All, > > What is the C-standard expected result for referring the > 'uninitialized element' of the partial initialized automatic array ? > > Is the result defined as 0 by standard? If yes, then, why is that so? Yes, it is defined as 0, probably because it is easy to implement the entire object as either fully initialized or not initialized. There would be a lot of work and little payoff for leaving unspecified elements within an aggregate unchanged. -- Thad |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
On Wed, 28 Nov 2007 22:00:31 -0800 (PST), Nishu
<naresh.attri@gmail.com> wrote: >Hi All, > >What is the C-standard expected result for referring the >'uninitialized element' of the partial initialized automatic array ? > >/*************************/ >#include <stdio.h> > >int main(void) >{ > int arr[2][3][4] = { 0, 1, 2, 3, > 4, 5, 6, 7, > 8, 9, 10, 11, > > 12, 13, 14, 15, > 16, 17, 18, 19, > 20, 21 > } ; > > printf(" arr[1][2][2] = %d \n", arr[1][2][3]); Is one of the two final subscripts a typo or am I missing some hidden meaning? > > return 0; >} > >/*************************/ > >Is the result defined as 0 by standard? If yes, then, why is that so? When an aggregate object is initialized with a list of initialization values that does not fill up the entire object, any remaining portions of the object are initialized as if by assignment with 0. If any remaining portion is itself an aggregate, apply this rule recursively. There is an exception for unions: only the first member of the union is initialized. So the short answer to your first question is yes, it is specified as 0 by the standard. For the second question, you may get an answer in a newsgroup that discusses the standard. I would guess that this simply put the official stamp of approval on what was the common practice at the time the first C standard was being drafted. It also provides a very convenient shorthand int x[100] = {1,2,3}; is much easier on the eyes and keyboard than int x[100] = {1,2,3,0,0,0,0,0,0,... Remove del for email |
|
![]() |
| Outils de la discussion | |
|
|