C语言 为什么 sizeof("") 等价于 1 而 sizeof(NULL) 等价于 c 语言中的 4?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4141666/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 06:58:21  来源:igfitidea点击:

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

c

提问by Jagan

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

为什么在c语言中sizeof("")相当于1而sizeof(NULL)相当于4?

回答by GManNickG

A string literal is an arrayof 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 charwith the value 0. The type is char[1], and sizeof(char)is always one; thereforesizeof(char[1])is always one.

文字""是一个数组,其中包含一个char值为0。类型为char[1],并且sizeof(char)始终为 1;因此sizeof(char[1])总是一。

In C, NULLis 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. NULLmay also expand to an integer of some type of the value 0, and you'd get the size of that instead.

在 C 中,NULL是实现定义的,通常是((void*)0). 的大小void*您的具体实现,是4。这可能是因您运行的平台上不同的数字。NULL也可以扩展为值 0 的某种类型的整数,并且您会得到它的大小。

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

*文字不是指针,数组不是指针,指针在这部分问题中不起作用。

回答by Adam Rosenfield

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

空字符串的""类型为char[1]“数组 1 char”。它不是一个指针,正如大多数人所相信的那样。它可以衰减为指针,因此任何时候都需要指向的指针char,您可以改用 的数组char,该数组将衰减为指向其第一个元素的指针。

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

由于sizeof(char)is 1(根据定义),因此我们有sizeof("")is sizeof(char[1]),即 1*1 = 1。

In C, NULLis 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) == 4and sizeof(void*) == 8, which means you can't depend on what sizeof(NULL)is.

在 C 中,NULL是“实现定义的空指针常量”(C99 §7.17.3)。“空指针常量”被定义为值为 0 的整数表达式,或这样的表达式转换为类型void *(C99 §6.3.2.3.3)。所以 的实际值sizeof(NULL)是实现定义的:你可能得到sizeof(int),或者你可能得到sizeof(void*)。在64位系统上,你经常sizeof(int) == 4sizeof(void*) == 8,这意味着你不能依靠什么sizeof(NULL)

Also note that most C implementations define NULLas ((void*)0)(though this is not required by the standard), whereas most C++ implementations just define NULLas 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).

另请注意,大多数 C 实现定义NULL((void*)0)(尽管标准不要求这样做),而大多数 C++ 实现仅定义NULL为普通的0. 这意味着sizeof(NULL)can 和 will的值会根据代码是编译为 C 还是 C++(例如,在 C 和 C++ 源文件之间共享的头文件中的代码)而变化。所以不要依赖sizeof(NULL)

回答by swatkat

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').

C 中的 NULL 定义为 (void*)0。由于它是一个指针,因此需要 4 个字节来存储它。并且,"" 是 1 个字节,因为该“空”字符串具有 EOL 字符 ('\0')。

回答by Espen

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

sizeof(NULL) 不是什么,而是指向地址 0 的指针,在 32 位系统上的指针需要 4 个字节。

回答by James Anderson

"" --> 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.

"" --> Cstrings 按照惯例以 x'00' 空字符终止,因此文字 "" 由一个字符 x'00' 组成,并且有 1 个字节。

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.

NULL 默认是一个空指针,在特定的 32 位机器上,它在不同平台上的大小为 4 个字节,可能是 1、2、3、4、6 或 8。甚至可能是 5 或 7,但我从未遇到过 40位或 56 位寻址。此外,在一些较旧的体系结构中,可能会有额外的位与指针相关联,以指示数据、指令、设备缓冲区存储等。