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


Groups > comp.lang.c > #9306

Re: char array initialization

From Ian Collins <ian-news@hotmail.com>
Newsgroups comp.lang.c
Subject Re: char array initialization
Date 2011-08-13 11:24 +1200
Message-ID <9alr1fFhn3U1@mid.individual.net> (permalink)
References <j22mak$5ss$1@news.ett.com.ua> <9akfchFbs7U1@mid.uni-berlin.de> <j249n8$85q$1@speranza.aioe.org>

Show all headers | View raw


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

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

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

csiph-web