Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 25 Feb 2010 23:07:15 +0100
From:      "Matthias Andree" <matthias.andree@gmx.de>
To:        =?utf-8?Q?Dag-Erling_Sm=C3=B8rgrav?= <des@des.no>, "Andrey Zonov" <andrey.zonov@gmail.com>
Cc:        freebsd-hackers@freebsd.org
Subject:   Re: 2 bytes allocated problems
Message-ID:  <op.u8peedxy1e62zd@merlin.emma.line.org>
In-Reply-To: <86sk8qpfx8.fsf@ds4.des.no>
References:  <983a1cf21002240544s59006035ifbf0ef7eb045e44f@mail.gmail.com> <86eikar7gv.fsf@ds4.des.no> <4B858007.1000008@gmail.com> <86sk8qpfx8.fsf@ds4.des.no>

next in thread | previous in thread | raw e-mail | index | archive | help
Am 24.02.2010, 20:55 Uhr, schrieb Dag-Erling Smørgrav <des@des.no>:

> Why is there a 0 after the 'i'?  Because when you write "abcdefghi", the
> compiler actually stores "abcdefghi\0".  That's the definition of
> "string" in C: a sequence of characters immediately followed by a 0.  If
> you don't want the 0 there, you have to do something like this:
>
>     char a[9] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i' };

   char a[9] = "abcdefghi";

suffices. The compiler knows there isn't room for the terminal '\0' and  
omits it.

   char a[] = "abcdefghi";

would append the implicit \0 and reserve 10 bytes for a.

> but then you don't have a string, just an array of 9 chars.

Not that the compiler itself could/would tell the difference after  
initialization, or that it would even care. It's the library functions  
that expect strings that care about the \0.

Beyond that, I recommend a C book to Andrey.

-- 
Matthias Andree



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?op.u8peedxy1e62zd>