开发者

why sizeof("") is equivalent to 1 and sizeof(NULL) is equivalent to 4 in c-language?

why sizeof("") is equivalent to 1 and 开发者_如何学Csizeof(NULL) is equivalent to 4 in c-language ?


A string literal is an array of characters* (with static storage), which contains all the characters in the literal along with a terminator. The size of an array is the size of the element multiplied by the number of elements in the array.

The literal "" is an array that consists of one char with the value 0. The type is char[1], and sizeof(char) is always one; thereforesizeof(char[1]) is always one.

In C, NULL is implementation-defined, and is often ((void*)0). The size of a void*, on your particular implementation, is 4. It may be a different number depending on the platform you run on. NULL may also expand to an integer of some type of the value 0, and you'd get the size of that instead.

*A literal is not a pointer, arrays are not pointers, pointers do not play a role in this part of the question.


The empty string "" has type char[1], or "array 1 of char". It is not a pointer, as most people believe. It can decay into a pointer, so any time a pointer to char is expected, you can use an array of char instead, and the array will decay into a pointer to its first element.

Since sizeof(char) is 1 (by definition), we therefore have sizeof("") is sizeof(char[1]), which is 1*1 = 1.

In C, NULL is an "implementation-defined null pointer constant" (C99 §7.17.3). A "null pointer constant" is defined to be an integer expression with the value 0, or such an expression cast to type void * (C99 §6.3.2.3.3). So the actual value of sizeof(NULL) is implementation-defined: you might get sizeof(int), or you might get sizeof(void*). On 64-bit systems, you often have sizeof(int) == 4 and sizeof(void*) == 8, which means you can't depend on what sizeof(NULL) is.

Also note that most C implementations define NULL as ((void*)0) (though this is not required by the standard), whereas most C++ implementations just define NULL as a plain 0. This means that the value of sizeof(NULL) can and will change depending on if code is compiled as C or as C++ (for example, code in header files shared between C and C++ source files). So do not depend on sizeof(NULL).


NULL in C is defined as (void*)0. Since it's a pointer, it takes 4 bytes to store it. And, "" is 1 byte because that "empty" string has EOL character ('\0').


sizeof(NULL) is not nothing, but a pointer to the address 0, and a pointer on a 32 bit system takes 4 bytes.


"" --> Cstrings are terminated by convention with a x'00' null character so literal "" consists of one character x'00' and has sie of 1 byte.

NULL is by default a null pointer which on particular 32 bit machines has a size of four bytes on different platforms it could br 1,2,3,4,6 or 8. Maybe even 5 or 7 but I have never come accross a 40 bit or 56 bit addressing. Also in some older architectures there may be extra bits associated with a pointer to indicate data vs. instruction vs. device buffer storage etc.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜