linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Glynn Clements <glynn@gclements.plus.com>
To: Randi Botse <nightdecoder@gmail.com>
Cc: Michal Nazarewicz <mina86@tlen.pl>, linux-c-programming@vger.kernel.org
Subject: Re: C Question
Date: Tue, 1 Jun 2010 14:18:10 +0100	[thread overview]
Message-ID: <19461.2194.469920.784076@cerise.gclements.plus.com> (raw)
In-Reply-To: <AANLkTik-eE99MeOMhJKcINHwzyzT_BcduarrMLVWvCDs@mail.gmail.com>


Randi Botse wrote:

> >> Sorry but, i just having another question, consider this code:
> >>
> >> const char *my_strings[] = {
> >>     [0] = "index0",
> >>     [4] = "index4",
> >>     [6] = "index6"
> >> };
> >
> > NULLs will be written at indexes 1, 2, 3 and 5.  There will be total of
> > 7 pointers (the last one will point to "index6").
> 
> That's clear now, i was though the 'unindexed' strings (in this case
> index 1, 2, 3, and 5) remains unintialized (not initialized to NULL).

If you specify an initialiser, the entire object is initialised. If
you specify an incomplete initialiser, the unspecified elements are
initialised to the appropriate zero value (0, 0.0, '\0', NULL, etc).

The C99 standard says (6.7.8p21):

       [#21]  If  there  are fewer initializers in a brace-enclosed
       list than there are elements or members of an aggregate,  or
       fewer  characters  in a string literal used to initialize an
       array of known size than there are elements  in  the  array,
       the   remainder   of  the  aggregate  shall  be  initialized
       implicitly the same as  objects  that  have  static  storage
       duration.

This applies to both traditional C89 sequential initialisers and C99
designated initialisers. It allows you to initialise large arrays or
structures without having to specify every element; e.g.:

	int array[1000] = {0};
	struct foo f = {0};

will initialise the entire array or structure to zero.

-- 
Glynn Clements <glynn@gclements.plus.com>
--
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

      reply	other threads:[~2010-06-01 13:18 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-05-25 10:08 C Question Randi Botse
2010-05-25 10:19 ` Xiaotian Feng
     [not found]   ` <AANLkTinTu94E7guvzjySpGjht2Hjw4aop4vRIVpHo9UL@mail.gmail.com>
2010-05-25 10:49     ` Randi Botse
2010-05-25 11:18 ` Uriel Corfa
2010-05-25 11:41 ` Michal Nazarewicz
2010-05-30 10:41   ` Randi Botse
2010-05-30 12:38     ` Michal Nazarewicz
2010-06-01 11:59       ` Randi Botse
2010-06-01 13:18         ` Glynn Clements [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=19461.2194.469920.784076@cerise.gclements.plus.com \
    --to=glynn@gclements.plus.com \
    --cc=linux-c-programming@vger.kernel.org \
    --cc=mina86@tlen.pl \
    --cc=nightdecoder@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).