C语言 sizeof() 如何处理 char*(指针变量)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17298172/
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
How does sizeof() work for char* (Pointer variables)?
提问by cloudyFan
I have a C code:
我有一个 C 代码:
char s1[20];
char *s = "fyc";
printf("%d %d\n", sizeof(s1), sizeof(s));
return 0;
It returns
它返回
20 8
I'm wondering how does the 8 come from, thanks!
我想知道8是怎么来的,谢谢!
回答by Nobilis
sizeof(s1)is the size of the array in memory, in your case 20 chars each being 1 byte equals 20.
sizeof(s1)是内存中数组的大小,在您的情况下,20 个字符每个 1 字节等于 20。
sizeof(s)is the size of the pointer. On different machines it can be a different size. On mine it's 4.
sizeof(s)是指针的大小。在不同的机器上,它可以是不同的大小。我的是4。
To test different type sizes on your machine you can just pass the type instead of a variable like so printf("%zu %zu\n", sizeof(char*), sizeof(char[20]));.
要在您的机器上测试不同的类型大小,您可以只传递类型而不是像这样的变量printf("%zu %zu\n", sizeof(char*), sizeof(char[20]));。
It will print 4and 20respectively on a 32bit machine.
它将分别在 32 位机器上打印4和20。
回答by Yu Hao
sizeof(char *)is the size of the pointer, so normally 4 for 32-bit machine, and 8 for 64-bit machine.
sizeof(char *)是指针的大小,所以通常 32 位机器为 4,64 位机器为 8。
sizeofan array, on the other hand, outputs the size of the array, in this case, 20*sizeof(char) = 20
sizeof另一方面,数组输出数组的大小,在本例中为 20*sizeof(char) = 20
One more thing, you should use %zufor size_ttype in printfformat.
还有一件事,你应该使用%zufor size_ttype in printfformat。
printf("%zu %zu\n", sizeof(s1), sizeof(s));
回答by zakinster
The sizeofoperator returns the size of a type. The operand of sizeofcan either be the parenthesized name of a typeor an expressionbut in any case, the size is determined from the type of the operand only.
该sizeof运营商返回的大小类型。的操作数sizeof可以是类型或表达式的括号名称,但在任何情况下,大小仅由操作数的类型决定。
sizeof s1is thus stricly equivalent to sizeof (char[20])and returns 20.
sizeof s1因此严格等价于sizeof (char[20])并返回20。
sizeof sis stricly equivalent to sizeof (char*)and returns the size of a pointer to char(64 bitsin your case).
sizeof s严格等同于sizeof (char*)并返回指针的大小char(在您的情况下为64 位)。
If you want the length of the C-string pointed by s, you could use strlen(s).
如果您想要由 指向的 C 字符串的长度s,您可以使用strlen(s).
回答by Adam Stelmaszczyk
8 is the size of a pointer, an address. On 64 bit machine it has 8 bytes.
8 是指针的大小,一个地址。在 64 位机器上它有 8 个字节。
回答by Chris Hajduk
If you are on a 64 bit computer, the memory addresses are 64 bit, therefore a 64 bit (8 bytes x 8 bits per byte) numeric value must be used to represent the numeric pointer variable (char*).
如果您使用的是 64 位计算机,则内存地址是 64 位,因此必须使用 64 位(8 字节 x 每字节 8 位)数值来表示数值指针变量 (char*)。
In other words, sizeof() works the same way for pointers as for standard variables. You just need to take into account the target platform when using pointers.
换句话说, sizeof() 对指针的工作方式与标准变量的工作方式相同。您只需要在使用指针时考虑目标平台。

