can i initialize a char array variable like this

F

Flo

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.

Greetings

Flo
 
B

Bart

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.

Not so. All other elements will be _default initialized_ which is not
the same as _uninitialized_. The default initalizer for built-in types
is zero.

Regards,
Bart.
 
V

Victor Bazarov

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, [...]

That is incorrect. All elements that are not given an explicit
initialiser are *zero-initialised*.

V
 
C

Clark S. Cox III

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,

That is simply not true, all of the other elements are default
initialized; which for integer types (like char) means that they are all
initialized to zero. If your compiler does not do this, then it is broken.

In the following:

char a[256] = {1};

The first element is initialized to (1), while all others are
initialized to (0).
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.

I wouldn't.
 
K

Kai-Uwe Bux

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,

really?

[8.5.1/7] If there are fewer initializers in the list than there are members
in the aggregate, then each member not explicitly initialized shall be
value-initialized (8.5).

[8.5/5] ...
To value-initialize an object of type T means:
? if T is a class type (clause 9) with a user-declared constructor (12.1),
then the default constructor for T is called (and the initialization is
ill-formed if T has no accessible default constructor);
? if T is a non-union class type without a user-declared constructor, then
every non-static data member and base-class component of T is
value-initialized;
? if T is an array type, then each element is value-initialized;
? otherwise, the object is zero-initialized

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.


Best

Kai-Uwe Bux
 
H

hankssong

if you want to initialize the char array with 0,
you can use a another way:
char a[256] = "";

thinktwice 写é“:
 
P

Pete Becker

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.

--

-- Pete

Author of "The Standard C++ Library Extensions: a Tutorial and Reference."
For more information about this book, see www.petebecker.com/tr1book.
 
R

Ron Natalie

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.

Untrue! When you specify fewer initializers to an aggregate than there
are elements, the rest are default initialized.
 
N

Noah Roberts

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.

WRONG!!!

Ok, I have nothing new to add, I just wanted to join in with the rest
of the hecklers and make sure you understand how very mistaken you
really are.
 
O

Old Wolf

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.
 
P

Pete Becker

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.

Citation, please? 8.5.2 does not say that. This is not "aggregate
initialization," so 8.5.1 does not apply.

--

-- Pete

Author of "The Standard C++ Library Extensions: a Tutorial and Reference."
For more information about this book, see www.petebecker.com/tr1book.
 
K

Kai-Uwe Bux

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.

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?
This is not "aggregate initialization," so 8.5.1 does not apply.

That, I can see.


Best

Kai-Uwe Bux
 
P

Pete Becker

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?

Seems like it. If the char array is initialized from a literal string
then it's holding a C-style string, and there's no reason to look at
anything after the terminating null character.

--

-- Pete

Author of "The Standard C++ Library Extensions: a Tutorial and Reference."
For more information about this book, see www.petebecker.com/tr1book.
 
O

Old Wolf

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.

You're right that 8.5.1 doesn't apply (despite this being the
initialization of an aggregate), and 8.5.2 doesn't mention the
characters after the 0-terminator.

I notice that 8.5.1#4 appears to conflict with 8.5.2#1. It says:

An array of unknown size initialized with a brace
enclosed initializerlist containing n initializers, where n
shall be greater than zero, is defined as having n
elements (8.3.4).

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" };

should also try to define array as having 1 element, and
initialize that element with a string literal, which should
cause an error since a string literal isn't a valid initailizer
for a single char -- in exactly the same way that
int array[] = { "string" };

fails. I think 8.5.1#4 should have an explicit mention that
this clause doesn't apply to initialization of character arrays
from a string literal.
 
P

Pete Becker

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" };

8.5.1 doesn't apply to initialization from a string literal. 8.5.2
controls that. The first rule of statutory construction is "begin at the
beginning." In this case, the beginning is the opening paragraphs of
8.5, in particular, 8.5/14.
 

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

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top