Hello. This seems odd to me. I have the following:

#define it "\x0\x0\x0\x0\x0\x0\x0\x0"

If I do:

DbgPrint("The size of it = 0x%x\n", sizeof(it));

This prints out:
The size of it = 0x9

Shouldn't this value be 8?

Thanks,
John

Re: sizeof ? by Maxim

Maxim
Fri Nov 05 08:18:17 CST 2004

sizeof() of a constant string includes the trailing zero added by the
compiler. This is a well known C and C++ feature.

--
Maxim Shatskih, Windows DDK MVP
StorageCraft Corporation
maxim@storagecraft.com
http://www.storagecraft.com

"John Thompson" <johnthompson1@hotmail.com> wrote in message
news:418b8a4e$1@news.xetron.com...
> Hello. This seems odd to me. I have the following:
>
> #define it "\x0\x0\x0\x0\x0\x0\x0\x0"
>
> If I do:
>
> DbgPrint("The size of it = 0x%x\n", sizeof(it));
>
> This prints out:
> The size of it = 0x9
>
> Shouldn't this value be 8?
>
> Thanks,
> John
>
>



Re: sizeof ? by John

John
Fri Nov 05 08:28:26 CST 2004


"John Thompson" <johnthompson1@hotmail.com> wrote in message
news:418b8a4e$1@news.xetron.com...
> Hello. This seems odd to me. I have the following:
>
> #define it "\x0\x0\x0\x0\x0\x0\x0\x0"
>
> If I do:
>
> DbgPrint("The size of it = 0x%x\n", sizeof(it));
>
> This prints out:
> The size of it = 0x9
>
> Shouldn't this value be 8?
>
> Thanks,
> John
>
>

Sorry to bother everyone. I just found out that #define's are NULL
terminated. Therefore, there really are nine characters.

-- John



Re: sizeof ? by Maxim

Maxim
Fri Nov 05 08:26:36 CST 2004

> Sorry to bother everyone. I just found out that #define's are NULL
> terminated. Therefore, there really are nine characters.

Not #define, but the constant string.

--
Maxim Shatskih, Windows DDK MVP
StorageCraft Corporation
maxim@storagecraft.com
http://www.storagecraft.com



Re: sizeof ? by Peter

Peter
Fri Nov 05 10:18:40 CST 2004

Constant string are just null terminated arrays of characters. Thus
WCHAR s[] = "foo";

is more or less the same as
const WCHAR s[] = {'f', 'o', 'o', '\0'};

The nice thing about using quoted strings rather than arrays is the compiler
will instantiate them for you, so you can take the address of them
trivially. For example:
PWCHAR s = "foo";

would be the same as:
const WCHAR _s[] = {'f', 'o', 'o', '\0'};
PWCHAR s = _s; // _s is the same as &(_s[0])

without burning the symbol name _s. The compiler can also eliminate
duplicate string constants, which it won't do with the array notation.

-p

--
This posting is provided "AS IS" with no warranties, and confers no rights.
"John Thompson" <johnthompson1@hotmail.com> wrote in message
news:418b8c76$1@news.xetron.com...
>
> "John Thompson" <johnthompson1@hotmail.com> wrote in message
> news:418b8a4e$1@news.xetron.com...
>> Hello. This seems odd to me. I have the following:
>>
>> #define it "\x0\x0\x0\x0\x0\x0\x0\x0"
>>
>> If I do:
>>
>> DbgPrint("The size of it = 0x%x\n", sizeof(it));
>>
>> This prints out:
>> The size of it = 0x9
>>
>> Shouldn't this value be 8?
>>
>> Thanks,
>> John
>>
>>
>
> Sorry to bother everyone. I just found out that #define's are NULL
> terminated. Therefore, there really are nine characters.
>
> -- John
>
>