char array initialization

R

Rox

Hi group,

For array initialization, I found this in [n1539] 6.7.9

14.
An array of character type may be initialized by a character string literal
or UTF−8 string
literal, optionally enclosed in braces. Successive bytes of the string
literal (including the
terminating null character if there is room or if the array is of unknown
size) initialize the
elements of the array.
19 ...
all subobjects that are not initialized explicitly shall be initialized
implicitly the same as
objects that have static storage duration.
and

So I presume:

char c[10] = "";

Will initialize c into 0. Just like memset do.
It's such a neat way to initial a string that I can't believe it!
Am I right?

PS: I verified this on several gcc versions, both gcc and g++, and it just
WORK

Rox
 
J

Jens Thoms Toerring

Rox said:
So I presume:
char c[10] = "";
Will initialize c into 0. Just like memset do.
It's such a neat way to initial a string that I can't believe it!
Am I right?

Yes, it has been that way for a long time. In C89 (3.5.7) you
have:

| If there are fewer initializers in a list than there are members of
| an aggregate, the remainder of the aggregate shall be initialized
| implicitly the same as objects that have static storage duration.

If I'm not completely mistakent his means that this trick does
not only work for char arrays but for all kinds of arrays. So

int i[ 10 ] = { 1 };

will initialize the first element of i to 1 and the rest to 0.
(And if you use 0 as the only initializer you will get an array
with all 10 elements set to 0.)

Your example of

char c[ 10 ] = "";

is just a special case of this since it's equivalent to

char c[ 10 ] = { '\0' };

And, a more complex example directly taken from C89

| float z[4][3] = {
| { 1 }, { 2 }, { 3 }, { 4 }
| };
|
| initializes the first column of z as specified and initializes the
| rest with zeros.

I would expect this to hold also for structures, so that

struct { int i; double d; } s = { 1 };

would have the effect of setting s.i to 1 and s.d to 0.0.
This results in a warning when using gcc which complains about
a missing initializer for s.d (but seems to do the expected).
I guess gcc is a bit overzealous here at the warning level I
am using (but then a compiler is free to warn about anything
it doesn't like).
Regards, Jens
 
P

Paul N

Rox said:
So I presume:
char c[10] = "";
Will initialize c into 0. Just like memset do.
It's such a neat way to initial a string that I can't believe it!
Am I right?

If I'm not completely mistakent his means that this trick does
not only work for char arrays but for all kinds of arrays. So

int i[ 10 ] = { 1 };

will initialize the first element of i to 1 and the rest to 0.
(And if you use 0 as the only initializer you will get an array
with all 10 elements set to 0.)

While you're both right (unless I too am mistaken!) I think Rox's
result is more surprising. I think one would expect initializing an
int array to initialize all the elements of that array. Whereas, if a
character array is intended to be initialised to a particular string,
one might expect that putting all the characters of the string into
the array, plus a zero to show the end, to be a sufficient
initialisation; one might not expect it to fill in all the other chars
as well.
 
J

James Kuyper

Rox said:
For array initialization, I found this in [n1539] 6.7.9
[snip]

Keep in mind that n1539 is a draft of the upcoming C201X standard. It's
very useful if you want to see what's coming in future versions of the
language, but it describes a version of C that isn't yet official and
has not yet been implemented.

For the current version of the language, see
<http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf>.

This particular feature, however, has been supported for decades. I
believe it predates the first version of the C standard, which came out
in 1989.
 
A

Alan Curry

Rox said:
So I presume:
char c[10] = "";
Will initialize c into 0. Just like memset do.
It's such a neat way to initial a string that I can't believe it!
Am I right?

Yes, it has been that way for a long time. In C89 (3.5.7) you
have:

| If there are fewer initializers in a list than there are members of
| an aggregate, the remainder of the aggregate shall be initialized
| implicitly the same as objects that have static storage duration.

Which I look at as a historical misfeature; there's no way to initialize
a local string without also wasting a bunch of cycles zero-filling the
bytes after the terminator. It would have been nice if there was a way
to write an initializer equivalent to

char buf[4096];
strcpy(buf, "some short string");
 
J

Jens Thoms Toerring

Alan Curry said:
Rox said:
So I presume:
char c[10] = "";
Will initialize c into 0. Just like memset do.
It's such a neat way to initial a string that I can't believe it!
Am I right?

Yes, it has been that way for a long time. In C89 (3.5.7) you
have:

| If there are fewer initializers in a list than there are members of
| an aggregate, the remainder of the aggregate shall be initialized
| implicitly the same as objects that have static storage duration.
Which I look at as a historical misfeature; there's no way to initialize
a local string without also wasting a bunch of cycles zero-filling the
bytes after the terminator. It would have been nice if there was a way
to write an initializer equivalent to
char buf[4096];
strcpy(buf, "some short string");

My (uneducated) guess is that the idea might have been not to
allow a "partial initialization" of an object, i.e. either don't
initialize it at all or do a complete job of it. Looks not so
reasonable for a char array (if it's used for a possibly shorter
string) but more sensible for other kinds of arrays (or struc-
tures) - and then having another special case for char arrays
would have complicated things unnecessary when a strcpy() or
memcpy() can be used for that purpose rather easily. But, as
I said, this is pure guesswork...

Regards, Jens
 
I

Ian Collins

Rox said:
So I presume:
char c[10] = "";
Will initialize c into 0. Just like memset do.
It's such a neat way to initial a string that I can't believe it!
Am I right?

Yes, it has been that way for a long time. In C89 (3.5.7) you
have:

| If there are fewer initializers in a list than there are members of
| an aggregate, the remainder of the aggregate shall be initialized
| implicitly the same as objects that have static storage duration.

Which I look at as a historical misfeature; there's no way to initialize
a local string without also wasting a bunch of cycles zero-filling the
bytes after the terminator. It would have been nice if there was a way
to write an initializer equivalent to

char buf[4096];
strcpy(buf, "some short string");

You have your answer - the code above!

How often in real code would you see

char buf[4096] = "some short string";

?
 
R

Rox

And from n1256, I got the same conclusion.

That's a lot.

Rox

"Keith Thompson" written message
Rox said:
For array initialization, I found this in [n1539] 6.7.9
[snip]

Keep in mind that n1539 is a draft of the upcoming C201X standard. It's
very useful if you want to see what's coming in future versions of the
language, but it describes a version of C that isn't yet official and
has not yet been implemented.

For the current version of the language, see
<http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf>.
 

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
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top