By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,939 Members | 1,546 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,939 IT Pros & Developers. It's quick & easy.

can i initialize a char array variable like this

P: n/a
char a[256] = { 0 }
is it ok?

Sep 7 '06 #1
Share this Question
Share on Google+
15 Replies


P: n/a
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

Sep 7 '06 #2

P: n/a
Flo wrote:
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.

Sep 7 '06 #3

P: n/a
Flo wrote:
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Sep 7 '06 #4

P: n/a
Flo wrote:
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.
--
Clark S. Cox III
cl*******@gmail.com
Sep 7 '06 #5

P: n/a
Flo wrote:
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

Sep 7 '06 #6

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

thinktwice 写道:
char a[256] = { 0 }
is it ok?
Sep 7 '06 #7

P: n/a
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.

--

-- Pete

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

P: n/a
Flo wrote:
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.
>
Sep 7 '06 #9

P: n/a

Flo wrote:
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.

Sep 7 '06 #10

P: n/a
Pete Becker wrote:
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.

Sep 8 '06 #11

P: n/a
Old Wolf wrote:
Pete Becker wrote:
>>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.

--

-- Pete

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

P: n/a
Pete Becker wrote:
Old Wolf wrote:
>Pete Becker wrote:
>>>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
Sep 8 '06 #13

P: n/a
Kai-Uwe Bux wrote:
>
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.
Sep 8 '06 #14

P: n/a
Pete Becker wrote:
Old Wolf wrote:
Pete Becker wrote:
>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.

Sep 11 '06 #15

P: n/a
Old Wolf wrote:
>
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.
Sep 12 '06 #16

This discussion thread is closed

Replies have been disabled for this discussion.