partial initialized array

N

Nishu

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
 
S

santosh

Nishu said:
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.
 
J

Jack Klein

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
 
T

Thad Smith

Nishu said:
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.
 
B

Barry Schwarz

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top