[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c

char array initialization

Rox

8/12/2011 7:56:00 AM

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

8 Answers

jt

8/12/2011 10:59:00 AM

0

Rox <roxxette.cn@gmail.com> wrote:
> 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
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://t...

gw7rib

8/12/2011 2:40:00 PM

0

On Aug 12, 11:59 am, j...@toerring.de (Jens Thoms Toerring) wrote:
> Rox <roxxette...@gmail.com> wrote:
> > 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.

Keith Thompson

8/12/2011 3:47:00 PM

0

"Rox" <roxxette.cn@gmail.com> writes:
> 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/n12....

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.ne...
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

James Kuyper

8/12/2011 4:23:00 PM

0

On 08/12/2011 11:47 AM, Keith Thompson wrote:
> "Rox" <roxxette.cn@gmail.com> writes:
>> 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/n12....

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.

pacman

8/12/2011 10:33:00 PM

0

In article <9akfchFbs7U1@mid.uni-berlin.de>,
Jens Thoms Toerring <jt@toerring.de> wrote:
>Rox <roxxette.cn@gmail.com> wrote:
>> 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");

--
Alan Curry

jt

8/12/2011 10:53:00 PM

0

Alan Curry <pacman@kosh.dhis.org> wrote:
> In article <9akfchFbs7U1@mid.uni-berlin.de>,
> Jens Thoms Toerring <jt@toerring.de> wrote:
> >Rox <roxxette.cn@gmail.com> wrote:
> >> 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
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://t...

Ian Collins

8/12/2011 11:25:00 PM

0

On 08/13/11 10:33 AM, Alan Curry wrote:
> In article<9akfchFbs7U1@mid.uni-berlin.de>,
> Jens Thoms Toerring<jt@toerring.de> wrote:
>> Rox<roxxette.cn@gmail.com> wrote:
>>> 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";

?

--
Ian Collins

Rox

8/15/2011 7:34:00 AM

0

And from n1256, I got the same conclusion.

That's a lot.

Rox

"Keith Thompson" written message news:lnmxfe7htf.fsf@nuthaus.mib.org...

"Rox" <roxxette.cn@gmail.com> writes:
> 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/n12....

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.ne...
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"