C语言 C 中的 sizeof() 函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17704923/
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
sizeof() function in C
提问by Shivaji_Vidhale
main()
{
char a[] = "Visual C++";
char *b = "Visual C++";
printf("\n %d %d",sizeof(a),sizeof(b));
printf("\n %d %d",sizeof(*a),sizeof(*b));
}
sizeof(a)gives me output: 11( that is length of the string)
sizeof(a)给我输出:(11即字符串的长度)
Why is it so ?
Why isn't the output sizeof(a)=4since when I try to print ait gives me an address value and hence an integer?
为什么会这样?
为什么不是输出sizeof(a)=4因为当我尝试打印时a它给了我一个地址值,因此是一个整数?
回答by Vivek Maran
Whenever you refer to the name of the array in your program, It normally decays to a pointer to the first element of the array. One of the exception to this rule is the sizeofoperator. So when you consider the following code.
每当您在程序中引用数组的名称时,它通常会衰减为指向数组第一个元素的指针。此规则的一个例外是sizeof运算符。因此,当您考虑以下代码时。
int main()
{
char a[] = "Visual C++";
printf("sizeof(a)=%d\n",sizeof(a)); /* Here sizeof(a) indicates sizeof array */
printf("a=%p",a); /* Here the array name, passed as an argument to printf decays into a pointer of type (char *) */
return 0;
}
回答by Eric Postpischil
In the declaration char a[] = "Visual C++", ais an array of 11 char. So its size is 11 bytes.
在声明中char a[] = "Visual C++",a是一个 11 的数组char。所以它的大小是 11 个字节。
In the declaration char *b = "Visual C++", bis a pointer to char. So its size is four bytes (in the C implementation you are using).
在声明中char *b = "Visual C++",b是一个指向char. 所以它的大小是四个字节(在你使用的 C 实现中)。
In the expression printf("%s", a), ais also an array. However, it is automatically converted to a pointer to the first element of the array. So a pointer to charis passed to printf.
在表达式中printf("%s", a),a也是一个数组。但是,它会自动转换为指向数组第一个元素的指针。所以一个指向 的指针char被传递给printf。
This conversion happens automatically unless an array is the argument of &, sizeof, or _Alignofor is a string literal used to initialize an array of char. Because it happens automatically, people tend to think of array names as pointers. However, they are not.
除非阵列是的参数,这种转换会自动发生&,sizeof或_Alignof或者是用于初始化数组的字符串文字char。因为它自动发生,人们倾向于将数组名称视为指针。然而,他们不是。
Incidentally, sizeofis an operator, not a function.
顺便说一句,sizeof是一个运算符,而不是一个函数。
回答by AnthonyLambert
When sizeof is applied to the name of a static array (not an array allocated through malloc), the result is the size in bytes of the whole array. This is one of the few exceptions to the rule that the name of an array is converted to a pointer to the first element of the array, and is possible just because the actual array size is fixed and known at compile time, when sizeof operator is evaluated.
当 sizeof 应用于静态数组(不是通过 malloc 分配的数组)的名称时,结果是整个数组的大小(以字节为单位)。这是将数组名称转换为指向数组第一个元素的指针的规则的少数例外之一,并且可能仅仅因为实际数组大小是固定的并且在编译时已知,当 sizeof 运算符是评估。
回答by Crowman
There are lots of errors, here.
这里有很多错误。
"sizeof(a)gives me output: 11(length of the string)"
“sizeof(a)给我输出:(11字符串的长度)”
The length of the string is 10, not 11. sizeof(a)gives you the length of the array.
字符串的长度是10,不是11。sizeof(a)给你数组的长度。
"why is it so, why isn't the output sizeof(a)=4since when I try to print ait gives me an address value"
“为什么是这样,为什么不是输出sizeof(a)=4因为当我尝试打印时a它给了我一个地址值”
Here are two methods of "printing a" which do not "give you an address value":
这里有两种“打印a”方法,它们不会“给你一个地址值”:
puts(a);
and:
和:
printf("%s\n", a);
so your logic is flawed, and this is the source of your confusion. "Printing a" only "gives you an address value" when you explicitly or implicitly elect to do so.
所以你的逻辑有问题,这就是你困惑的根源。a当您明确或隐含地选择这样做时,“打印”只会“给您一个地址值”。
sizeof(a)gives 11 in this case because the C language defines the sizeofoperator to give you the size of an array when an array is the operand. This, I'd argue, is the most natural behavior people would expect, so presumably that's why it is defined as such.
sizeof(a)在这种情况下给出 11,因为sizeof当数组是操作数时,C 语言定义了运算符来为您提供数组的大小。我认为,这是人们期望的最自然的行为,所以大概这就是它被定义为这样的原因。
"and hence an integer."
“因此是一个整数。”
In any case, an address is an address, not an integer. At best you could argue that it ought to give you the size of a pointer, but certainly not the size of an integer.
在任何情况下,地址都是地址,而不是整数。充其量你可以争辩说它应该给你一个指针的大小,但肯定不是一个整数的大小。

