2 bytes allocated problems
Matthias Andree
matthias.andree at gmx.de
Thu Feb 25 22:07:21 UTC 2010
Am 24.02.2010, 20:55 Uhr, schrieb Dag-Erling Smørgrav <des at 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
More information about the freebsd-hackers
mailing list