initialize char string

Y

Yang Lee

hi
is this correct

char teststr[100]="\0";

or is there another method to initialise;
thanks
lee
 
C

Christian Kandeler

Yang said:
hi
is this correct

char teststr[100]="\0";

or is there another method to initialise;

char teststr[100] = "" suffices, because the string literal already has a
NUL character at the end (in position 0, in this case).


Christian
 
I

infobahn

Yang said:
hi
is this correct

char teststr[100]="\0";

It works.
or is there another method to initialise;

The following both do the same thing, perhaps a touch more elegantly:

char teststr[100] = "";
char teststr[100] = {0};
 
G

gooch

infobahn said:
Yang said:
hi
is this correct

char teststr[100]="\0";

It works.
or is there another method to initialise;

The following both do the same thing, perhaps a touch more elegantly:

char teststr[100] = "";
This initializes the first element to a terminating null character and
does nothing to the rest of the array.
char teststr[100] = {0};
This one does not do the same thing. This one initializes the entire
array to zeros.
 
P

pete

gooch said:
Yang said:
hi
is this correct

char teststr[100]="\0";

It works.
or is there another method to initialise;

The following both do the same thing,
perhaps a touch more elegantly:

char teststr[100] = "";
This initializes the first element to a terminating null character and
does nothing to the rest of the array.
char teststr[100] = {0};
This one does not do the same thing. This one initializes the entire
array to zeros.

infobahn is right, gooch is wrong.
 
M

Michael Mair

pete said:
gooch said:
infobahn said:
Yang Lee wrote:

hi
is this correct

char teststr[100]="\0";

It works.


or is there another method to initialise;

The following both do the same thing,
perhaps a touch more elegantly:

char teststr[100] = "";

This initializes the first element to a terminating null character and
does nothing to the rest of the array.
char teststr[100] = {0};

This one does not do the same thing. This one initializes the entire
array to zeros.

infobahn is right, gooch is wrong.

To clarify that: C99, 6.7.8#20 (Initializers)
"If there are fewer initializers in a brace-enclosed list than there are
elements or members of an aggregate, or fewer characters in a string
literal used to initialize an array of known size than there are
elements in the array, the remainder of the aggregate shall be
initialized implicitly the same as objects that have static storage
duration."

That is, the second behaviour gooch was talking about (rest initialised
to zero) is always what we can expect.


Cheers
Michael
 
G

gooch

infobahn is right, gooch is wrong.

I stand corrected. I seem to remember reading this in the standard at
one point but I went to my document after reading your reply and there
it is just as you say.
 
M

Mabden

Michael Mair said:
char teststr[100] = {0};

This one does not do the same thing. This one initializes the entire
array to zeros.

infobahn is right, gooch is wrong.

To clarify that: C99, 6.7.8#20 (Initializers)
"If there are fewer initializers in a brace-enclosed list than there are
elements or members of an aggregate, or fewer characters in a string
literal used to initialize an array of known size than there are
elements in the array, the remainder of the aggregate shall be
initialized implicitly the same as objects that have static storage
duration."

That is, the second behaviour gooch was talking about (rest initialised
to zero) is always what we can expect.

What does this do:

char teststr[100] = {1,2,3};

Does teststr now have 1, 2, 3 then 97 zeros? Or 1, 2, and 98 threes? Or
what?
 
M

Michael Mair

Mabden said:
char teststr[100] = {0};

This one does not do the same thing. This one initializes the entire
array to zeros.

infobahn is right, gooch is wrong.

To clarify that: C99, 6.7.8#20 (Initializers)
"If there are fewer initializers in a brace-enclosed list than there
are

elements or members of an aggregate, or fewer characters in a string
literal used to initialize an array of known size than there are
elements in the array, the remainder of the aggregate shall be
initialized implicitly the same as objects that have static storage
duration."

That is, the second behaviour gooch was talking about (rest
initialised

to zero) is always what we can expect.


What does this do:

char teststr[100] = {1,2,3};

Does teststr now have 1, 2, 3 then 97 zeros? Or 1, 2, and 98 threes? Or
what?

Think hard, mock elsewhere.
Hint: Integer variables of static storage duration without an explicit
initialiser are initialised to 0, for arrays this applies elementwise.


-Michael
 
M

Mabden

Michael Mair said:
char teststr[100] = {0};

This one does not do the same thing. This one initializes the entire
array to zeros.

infobahn is right, gooch is wrong.

To clarify that: C99, 6.7.8#20 (Initializers)
"If there are fewer initializers in a brace-enclosed list than there
are

elements or members of an aggregate, or fewer characters in a string
literal used to initialize an array of known size than there are
elements in the array, the remainder of the aggregate shall be
initialized implicitly the same as objects that have static storage
duration."

That is, the second behaviour gooch was talking about (rest
initialised

to zero) is always what we can expect.


What does this do:

char teststr[100] = {1,2,3};

Does teststr now have 1, 2, 3 then 97 zeros? Or 1, 2, and 98 threes? Or
what?

Think hard, mock elsewhere.

I was not mocking, this time.
Hint: Integer variables of static storage duration without an explicit
initialiser are initialised to 0, for arrays this applies elementwise.

I was wondering if the array is initialized with the last value or zero.
Actually I meant to add a third option (Does is init with
1,2,3,1,2,3,1,2,3,1,2,3 for the size of the array?) but I posted too
quickly and saved myself some embarrassment... ooops!

I thought the standard might have meant that it would take whatever
partial initializers it was given and propagated them to the entire
array. But it seems it actually treats it like an automatic variable and
fills with zeros.

I live and learn from all your wonderful insights.
 

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,159
Messages
2,570,879
Members
47,417
Latest member
DarrenGaun

Latest Threads

Top