Y
Yang Lee
hi
is this correct
char teststr[100]="\0";
or is there another method to initialise;
thanks
lee
is this correct
char teststr[100]="\0";
or is there another method to initialise;
thanks
lee
Yang said:hi
is this correct
char teststr[100]="\0";
or is there another method to initialise;
Yang said:hi
is this correct
char teststr[100]="\0";
or is there another method to initialise;
This initializes the first element to a terminating null character andinfobahn 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 one does not do the same thing. This one initializes the entirechar teststr[100] = {0};
gooch said:This initializes the first element to a terminating null character andYang 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] = "";
does nothing to the rest of the array.This one does not do the same thing. This one initializes the entirechar teststr[100] = {0};
array to zeros.
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.
infobahn is right, gooch is wrong.
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?
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?
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.
Hint: Integer variables of static storage duration without an explicit
initialiser are initialised to 0, for arrays this applies elementwise.
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.