Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #9248 > unrolled thread

char array initialization

Started by"Rox" <roxxette.cn@gmail.com>
First post2011-08-12 15:55 +0800
Last post2011-08-15 15:34 +0800
Articles 9 — 7 participants

Back to article view | Back to comp.lang.c


Contents

  char array initialization "Rox" <roxxette.cn@gmail.com> - 2011-08-12 15:55 +0800
    Re: char array initialization jt@toerring.de (Jens Thoms Toerring) - 2011-08-12 10:59 +0000
      Re: char array initialization Paul N <gw7rib@aol.com> - 2011-08-12 07:39 -0700
      Re: char array initialization pacman@kosh.dhis.org (Alan Curry) - 2011-08-12 22:33 +0000
        Re: char array initialization jt@toerring.de (Jens Thoms Toerring) - 2011-08-12 22:52 +0000
        Re: char array initialization Ian Collins <ian-news@hotmail.com> - 2011-08-13 11:24 +1200
    Re: char array initialization Keith Thompson <kst-u@mib.org> - 2011-08-12 08:47 -0700
      Re: char array initialization James Kuyper <jameskuyper@verizon.net> - 2011-08-12 12:23 -0400
      Re: char array initialization "Rox" <roxxette.cn@gmail.com> - 2011-08-15 15:34 +0800

#9248 — char array initialization

From"Rox" <roxxette.cn@gmail.com>
Date2011-08-12 15:55 +0800
Subjectchar array initialization
Message-ID<j22mak$5ss$1@news.ett.com.ua>
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 

[toc] | [next] | [standalone]


#9252

Fromjt@toerring.de (Jens Thoms Toerring)
Date2011-08-12 10:59 +0000
Message-ID<9akfchFbs7U1@mid.uni-berlin.de>
In reply to#9248
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://toerring.de

[toc] | [prev] | [next] | [standalone]


#9257

FromPaul N <gw7rib@aol.com>
Date2011-08-12 07:39 -0700
Message-ID<29557214-f2e4-492c-acc9-73c8aa8f22f4@br5g2000vbb.googlegroups.com>
In reply to#9252
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.

[toc] | [prev] | [next] | [standalone]


#9303

Frompacman@kosh.dhis.org (Alan Curry)
Date2011-08-12 22:33 +0000
Message-ID<j249n8$85q$1@speranza.aioe.org>
In reply to#9252
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

[toc] | [prev] | [next] | [standalone]


#9304

Fromjt@toerring.de (Jens Thoms Toerring)
Date2011-08-12 22:52 +0000
Message-ID<9alp60Fnd5U1@mid.uni-berlin.de>
In reply to#9303
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://toerring.de

[toc] | [prev] | [next] | [standalone]


#9306

FromIan Collins <ian-news@hotmail.com>
Date2011-08-13 11:24 +1200
Message-ID<9alr1fFhn3U1@mid.individual.net>
In reply to#9303
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

[toc] | [prev] | [next] | [standalone]


#9262

FromKeith Thompson <kst-u@mib.org>
Date2011-08-12 08:47 -0700
Message-ID<lnmxfe7htf.fsf@nuthaus.mib.org>
In reply to#9248
"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/n1256.pdf>.

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

[toc] | [prev] | [next] | [standalone]


#9266

FromJames Kuyper <jameskuyper@verizon.net>
Date2011-08-12 12:23 -0400
Message-ID<j23k57$d9i$1@dont-email.me>
In reply to#9262
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/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.

[toc] | [prev] | [next] | [standalone]


#9422

From"Rox" <roxxette.cn@gmail.com>
Date2011-08-15 15:34 +0800
Message-ID<j2ai6h$1ouf$1@news.ett.com.ua>
In reply to#9262
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/n1256.pdf>.

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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.c


csiph-web