public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
From: "Jędrzej Dudkiewicz" <jedrzej.dudkiewicz@gmail.com>
To: gcc-help <gcc-help@gcc.gnu.org>
Subject: Re: odd behavior with Character Arrays
Date: Fri, 08 Aug 2008 12:10:00 -0000	[thread overview]
Message-ID: <ab4f06660808080237p463f53c7rd08043ebc81936fb@mail.gmail.com> (raw)
In-Reply-To: <c356fd4e0808080006saf00452o547658a6f025ac03@mail.gmail.com>

On Fri, Aug 8, 2008 at 9:06 AM, Rohit Arul Raj <rohitarulraj@gmail.com> wrote:
> On Fri, Aug 8, 2008 at 12:24 PM, Mateusz Loskot <mateusz@loskot.net> wrote:
> Hi,
>
> If i give the size of the array as 15, like "unsigned char n[15] =
> {'a', 'b', 'c','d'};" , then it is appending '\0'.

Yes, because you provide values only for four first chars.

> But if the size of the array is not given "unsigned char n[] ", then
> it is not appending '\0'.

Why should it? If you don't specify size of an array, compiler
calculates it from provided initializer (I'm not sure if this is the
proper name?), in this case - explicit array of characters. Note that
this is NOT a C-string - so it does not contain implicit '\0'
character. Change { 'a',.... } to "abcd" and '\0' will be there.

> Does that mean, that if the size of the array is specified, it appends
> '\0' and if it is not specified then it does not append '\0'?

Yes and no. Yes, because it happens so with values you provided above,
no, because if you specify size to be 4 and use the same initializer,
that is { 'a', 'b', 'c', 'd' }, all values are provided and there is
no need and room to use default initialization.

-- 
Jędrzej Dudkiewicz

I really hate this damn machine, I wish that they would sell it.
It never does just what I want, but only what I tell it.

  reply	other threads:[~2008-08-08  9:38 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-08-08  6:55 Rohit Arul Raj
2008-08-08  7:07 ` Mateusz Loskot
2008-08-08  9:38   ` Rohit Arul Raj
2008-08-08 12:10     ` Jędrzej Dudkiewicz [this message]
2008-08-08 13:00     ` Mateusz Loskot
2008-08-08 13:40       ` Mateusz Loskot
2008-08-08 15:01     ` John Fine
2008-08-09  1:09       ` Is this code wrong? John Fine
2008-08-09  2:44         ` Eljay Love-Jensen
2008-08-09 11:23           ` corey taylor
2008-08-09 14:57             ` John Fine
2008-08-08 15:38     ` odd behavior with Character Arrays Bob Plantz

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=ab4f06660808080237p463f53c7rd08043ebc81936fb@mail.gmail.com \
    --to=jedrzej.dudkiewicz@gmail.com \
    --cc=gcc-help@gcc.gnu.org \
    /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 read-only IMAP folder(s) and NNTP newsgroup(s).