Re: odd behavior with Character Arrays

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Fri, Aug 8, 2008 at 9:06 AM, Rohit Arul Raj <rohitarulraj@xxxxxxxxx> wrote:> On Fri, Aug 8, 2008 at 12:24 PM, Mateusz Loskot <mateusz@xxxxxxxxxx> 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, compilercalculates it from provided initializer (I'm not sure if this is theproper name?), in this case - explicit array of characters. Note thatthis 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 isno 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.

[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux