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


Groups > comp.lang.c > #9303

Re: char array initialization

From pacman@kosh.dhis.org (Alan Curry)
Newsgroups comp.lang.c
Subject Re: char array initialization
Date 2011-08-12 22:33 +0000
Organization Aioe.org NNTP Server
Message-ID <j249n8$85q$1@speranza.aioe.org> (permalink)
References <j22mak$5ss$1@news.ett.com.ua> <9akfchFbs7U1@mid.uni-berlin.de>

Show all headers | View raw


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

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