T
thinktwice
char a[256] = { 0 }
is it ok?
is it ok?
Flo said:Yes, it is ok form a syntax point of view if you append a semicolon.
char a[256] = { 0 } ;
However that initialises only the first element to 0, all other
elements are left uninitialized, what is probably not what you have
intended to do.
Flo said:Yes, it is ok form a syntax point of view if you append a semicolon.
char a[256] = { 0 } ;
However that initialises only the first element to 0, all other
elements are left uninitialized, [...]
Flo said:Yes, it is ok form a syntax point of view if you append a semicolon.
char a[256] = { 0 } ;
However that initialises only the first element to 0, all other
elements are left uninitialized,
what is probably not what you have
intended to do. If you like to initialize the whole array, you have to
initialize each element as in
char a[3] = { 'a', 'b', 0 } ;
For your huge array, i would use a loop or memset to initialize it.
Flo said:Yes, it is ok form a syntax point of view if you append a semicolon.
char a[256] = { 0 } ;
However that initialises only the first element to 0, all other
elements are left uninitialized,
what is probably not what you have intended to do. If you like to
initialize the whole array, you have to initialize each element as in
char a[3] = { 'a', 'b', 0 } ;
For your huge array, i would use a loop or memset to initialize it.
hankssong said:char a[256] = { 0 }
is it ok?
> if you want to initialize the char array with 0,
> you can use a another way:
> char a[256] = "";
>
Flo said:Yes, it is ok form a syntax point of view if you append a semicolon.
char a[256] = { 0 } ;
However that initialises only the first element to 0, all other
elements are left uninitialized, what is probably not what you have
intended to do.
Flo said:Yes, it is ok form a syntax point of view if you append a semicolon.
char a[256] = { 0 } ;
However that initialises only the first element to 0, all other
elements are left uninitialized, what is probably not what you have
intended to do. If you like to initialize the whole array, you have to
initialize each element as in
char a[3] = { 'a', 'b', 0 } ;
For your huge array, i would use a loop or memset to initialize it.
Pete said:hankssong said:char a[256] = { 0 }
is it ok?
if you want to initialize the char array with 0,
you can use a another way:
char a[256] = "";
That will set the first character to 0, which is fine if that's what's
needed. It's different from the first version, which sets all the
characters to 0.
Old said:Pete said:hankssong said:char a[256] = { 0 }
is it ok?
if you want to initialize the char array with 0,
you can use a another way:
char a[256] = "";
That will set the first character to 0, which is fine if that's what's
needed. It's different from the first version, which sets all the
characters to 0.
Huh? When initializing an aggregate, all members get
initialized.
In this particular case, "" has identical meaning to { '\0' },
and all members of the array will be set to 0.
Pete said:Old said:Pete said:hankssong wrote:
char a[256] = { 0 }
is it ok?
if you want to initialize the char array with 0,
you can use a another way:
char a[256] = "";
That will set the first character to 0, which is fine if that's what's
needed. It's different from the first version, which sets all the
characters to 0.
Huh? When initializing an aggregate, all members get
initialized.
In this particular case, "" has identical meaning to { '\0' },
and all members of the array will be set to 0.
Citation, please? 8.5.2 does not say that.
This is not "aggregate initialization," so 8.5.1 does not apply.
Kai-Uwe Bux said:I just read 8.5.2. It says: successive characters of the string-literal
initialize the members of the array; but I cannot find anything therein
that specifies what happens if the character array is longer than the
string literal (including the terminating 0). Is it up to the
implementation?
Pete said:Old said:Pete said:hankssong wrote:
char a[256] = "";
That will set the first character to 0, which is fine if that's what's
needed. It's different from the first version, which sets all the
characters to 0.
Huh? When initializing an aggregate, all members get
initialized.
Citation, please? 8.5.2 does not say that. This is not "aggregate
initialization," so 8.5.1 does not apply.
Old said:If the code is:
char *array[] = { "string" };
then it's clear that { "string" } is a brace-enclosed initializer
list containing 1 initializer, and array is defined as having
1 element. So, according to 8.5.1#4, the code
char array[] = { "string" };
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.