473,396 Members | 1,671 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

partial initialized array

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
Nov 29 '07 #1
4 5953
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.
Nov 29 '07 #2
On Wed, 28 Nov 2007 22:00:31 -0800 (PST), Nishu
<na**********@gmail.comwrote 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
Nov 30 '07 #3
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
Nov 30 '07 #4
On Wed, 28 Nov 2007 22:00:31 -0800 (PST), Nishu
<na**********@gmail.comwrote:
>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
Dec 1 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Philip Lawatsch | last post by:
Hi I'd like to implement some kind if type traits myself, but I have to support broken compilers (like visual studio) that do not support Partial Specialization. My first shot was something...
4
by: Steven T. Hatton | last post by:
I mistakenly set this to the comp.std.c++ a few days back. I don't believe it passed the moderator's veto - and I did not expect or desire anything different. But the question remains: ISO/IEC...
34
by: John | last post by:
This produces an initialized array to zero: int *i = new int() ; 004124B0 push ebp 004124B1 mov ebp,esp 004124B3 mov eax,dword ptr 004124B6 push eax...
8
by: Oskar Enoksson | last post by:
The following template specialization construct is rejected by g++ 4.1.2 while icc 9.1 silently accepts is (even with -strict-ansi). Which compiler is correct? template<typename T> struct x...
10
by: jirik | last post by:
Hi, I have a question concerning partly initialized array. I'll give an example of code: int array = { = 55}; Does compiler grants me any assurance, that other values (except with index 10)...
1
by: matthias.kessler | last post by:
I've got an issue understanding where my compiler places data. I'm developing for an embedded target, so using as little RAM as possible is very important. Anonymous strings are stored in ROM and I...
4
by: setiarakesh | last post by:
I have designed a socket Server and developed asynchronous server . It is working fine with 60 Clients which are connecting to ths Server running at Machine (2 GB RAM and OS is Windows 2003...
2
by: Peng Yu | last post by:
Hi, I'm wondering if there is a way to initialized a member array just as the initialization of a member variable? Or I have to initialized the array inside the function body of the constructor?...
50
by: arunajob | last post by:
Hi all, If I have a piece of code something like this void main(void) { char * p1="abcdefghijklmn"; ............................................. }
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.