Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > char array initialization

Reply
Thread Tools

char array initialization

 
 
Rox
Guest
Posts: n/a
 
      08-12-2011
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

 
Reply With Quote
 
 
 
 
Jens Thoms Toerring
Guest
Posts: n/a
 
      08-12-2011
Rox <> 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 ___
\__________________________ http://toerring.de
 
Reply With Quote
 
 
 
 
Paul N
Guest
Posts: n/a
 
      08-12-2011
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.
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      08-12-2011
"Rox" <> 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- <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"
 
Reply With Quote
 
James Kuyper
Guest
Posts: n/a
 
      08-12-2011
On 08/12/2011 11:47 AM, Keith Thompson wrote:
> "Rox" <> 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.
 
Reply With Quote
 
Alan Curry
Guest
Posts: n/a
 
      08-12-2011
In article <>,
Jens Thoms Toerring <> wrote:
>Rox <> 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
 
Reply With Quote
 
Jens Thoms Toerring
Guest
Posts: n/a
 
      08-12-2011
Alan Curry <> wrote:
> In article <>,
> Jens Thoms Toerring <> wrote:
> >Rox <> 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 ___
\__________________________ http://toerring.de
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      08-12-2011
On 08/13/11 10:33 AM, Alan Curry wrote:
> In article<>,
> Jens Thoms Toerring<> wrote:
>> Rox<> 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
 
Reply With Quote
 
Rox
Guest
Posts: n/a
 
      08-15-2011
And from n1256, I got the same conclusion.

That's a lot.

Rox

"Keith Thompson" written message news:...

"Rox" <> 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- <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"
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
length of 2D Array >> char **myString= (char **) malloc (sizeof (char *)); davidb C++ 0 09-01-2006 03:22 PM
(const char *cp) and (char *p) are consistent type, (const char **cpp) and (char **pp) are not consistent lovecreatesbeauty C Programming 1 05-09-2006 08:01 AM
Problem- strcat with char and char indexed from char array aldonnelley@gmail.com C++ 3 04-20-2006 07:32 AM
/usr/bin/ld: ../../dist/lib/libjsdombase_s.a(BlockGrouper.o)(.text+0x98): unresolvable relocation against symbol `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostre silverburgh.meryl@gmail.com C++ 3 03-09-2006 12:14 AM
char array initialization: Is 'char a[] = ("a")' valid ANSI C? Petter Reinholdtsen C Programming 20 11-22-2004 06:15 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57