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


Groups > comp.lang.c > #9304

Re: char array initialization

From jt@toerring.de (Jens Thoms Toerring)
Newsgroups comp.lang.c
Subject Re: char array initialization
Date 2011-08-12 22:52 +0000
Organization Freie Universitaet Berlin
Message-ID <9alp60Fnd5U1@mid.uni-berlin.de> (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


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

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