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?
10 2724
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
What kind of code is this?? -
int array[20] = {[10] = 55};
-
It's not C or C++.
Huh? What do you mean you don't specify any standard?
Adrian
I meant if compile it without -std switch
What kind of code is this?? -
int array[20] = {[10] = 55};
-
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
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
What kind of code is this?? -
int array[20] = {[10] = 55};
-
It's not C or C++.
It is C from the C99 standard. You can also initialialise parts of structs in a similar way: -
struct A {
-
int a;
-
char b[30];
-
double c;
-
char d[30];
-
};
-
-
struct A obj = { .b = "Hello", .d = { [29] = '\0' } };
-
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
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++.
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
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
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
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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:
|
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...
|
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 (...
|
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...
|
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...
|
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" };
....
};
|
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...
|
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...
|
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$) {
}
...
|
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...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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,...
|
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...
| |