473,385 Members | 1,766 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,385 software developers and data experts.

Partly initialized array default values in C

4
Hi, I have a question concerning partly initialized array. I'll give an example of code:

int array[20] = {[10] = 55};

Does compiler grants me any assurance, that other values (except with index 10) will have some particular value? I've read, that when using C99, other values will be 0, but what if I don't specify any standard?
Jun 1 '07 #1
10 2724
AdrianH
1,251 Expert 1GB
Hi, I have a question concerning partly initialized array. I'll give an example of code:

int array[20] = {[10] = 55};

Does compiler grants me any assurance, that other values (except with index 10) will have some particular value? I've read, that when using C99, other values will be 0, but what if I don't specify any standard?
Huh? What do you mean you don't specify any standard?

If you specify an initialiser, even if it is empty (i.e. {}) it will default the uninitialised values to 0 or NULL (NULL doen't necessarly mean 0 on all CPUs).


Adrian
Jun 2 '07 #2
weaknessforcats
9,208 Expert Mod 8TB
What kind of code is this??

Expand|Select|Wrap|Line Numbers
  1. int array[20] = {[10] = 55};
  2.  
It's not C or C++.
Jun 2 '07 #3
jirik
4
Huh? What do you mean you don't specify any standard?
Adrian
I meant if compile it without -std switch
Jun 2 '07 #4
jirik
4
What kind of code is this??

Expand|Select|Wrap|Line Numbers
  1. int array[20] = {[10] = 55};
  2.  
It's not C or C++.
Sorry, I thought it'd be much clearer, if I didn't specify anything else, just this declaration of an array.. I guess, this should be a correct snippet of C code
Jun 2 '07 #5
jirik
4
If you specify an initialiser, even if it is empty (i.e. {}) it will default the uninitialised values to 0 or NULL (NULL doen't necessarly mean 0 on all CPUs).
Adrian
Thanks, this is just what I wanted to hear
Jun 2 '07 #6
AdrianH
1,251 Expert 1GB
What kind of code is this??

Expand|Select|Wrap|Line Numbers
  1. int array[20] = {[10] = 55};
  2.  
It's not C or C++.
It is C from the C99 standard. You can also initialialise parts of structs in a similar way:
Expand|Select|Wrap|Line Numbers
  1. struct A {
  2.   int a;
  3.   char b[30];
  4.   double c;
  5.   char d[30];
  6. };
  7.  
  8. struct A obj = { .b = "Hello",  .d = { [29] = '\0' } };
  9.  
Anything not initialised is initialised to 0 or NULL. So initialising obj.d[29] to '\0' doesn't save you from not initialising obj.d[0-28].

However, if you do not specifiy an initialiser list, the the elements are not initialised UNLESS it is not automatic storage, in which case it is set to 0 or NULL.

I found this to be an interesting read.


Adrian
Jun 2 '07 #7
weaknessforcats
9,208 Expert Mod 8TB
It is C from the C99 standard. You can also initialialise parts of structs in a similar way:
Obviously I know only K&R C. Further research reveals Visual Studio.NET 2005 doesn't know about it either although there are various C99 doc pages all marked "unsupported".

I based my comment on Visual Studio failing to compile the snippet as either C or C++.
Jun 2 '07 #8
AdrianH
1,251 Expert 1GB
Obviously I know only K&R C. Further research reveals Visual Studio.NET 2005 doesn't know about it either although there are various C99 doc pages all marked "unsupported".

I based my comment on Visual Studio failing to compile the snippet as either C or C++.
Hey, a standard is a standard. If the compiler you are using doesn't conform to it, what can you do but not use the standard or use a compiler that does.


Adrian
Jun 2 '07 #9
Savage
1,764 Expert 1GB
Hey, a standard is a standard. If the compiler you are using doesn't conform to it, what can you do but not use the standard or use a compiler that does.


Adrian
I'm starting to hate all that mumbo jumbo about standards

Savage
Jun 2 '07 #10
AdrianH
1,251 Expert 1GB
I'm starting to hate all that mumbo jumbo about standards

Savage
Well, it is not so bad. At least the standards are incremental and not just willy nilly. ;)


Adrian
Jun 2 '07 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

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...
9
by: Gernot Frisch | last post by:
void foo(int* p) {} main() { foo( {1,2,3} ); } How can I do this? I have a class, that expects this as a constructor:
47
by: VK | last post by:
Or why I just did myArray = "Computers" but myArray.length is showing 0. What a hey? There is a new trend to treat arrays and hashes as they were some variations of the same thing. But they...
2
by: mohan | last post by:
According to page 20 & 21 "Unix Network Proramming" W>Richard Stevens the vale of argv and argc are in initialized read-only data. This must mean that this is not changable inside the program (...
4
by: Brian Henry | last post by:
I have an icon I want to draw onto the screen, but I want to streatch it out to be about 256x256 and make it about 75% transparent how would I go about this? do i need to convert it to a bitmap...
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...
14
by: Gary Wessle | last post by:
Hi how can I initialize a const char* myarr in a class constructor? class Myclass { const char* myarr = { "abc", "def" }; .... };
2
by: gallasr | last post by:
Hello, three years I made upgrade to newest PHP on my local PC and links stopped to work. This is the only site I use PHP so I`m just casual user of this language. Worked before, not working...
4
by: Nishu | last post by:
Hi All, What is the C-standard expected result for referring the 'uninitialized element' of the partial initialized automatic array ? /*************************/ #include <stdio.h> int...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.