怎样的sizeof()为字符*(指针变量)的工作? [英] How does sizeof() work for char* (Pointer variables)?
问题描述
我有一个C code:
I have a C code:
char s1[20];
char *s = "fyc";
printf("%d %d\n", sizeof(s1), sizeof(s));
return 0;
它返回
20 8
我想知道如何不来的,从8,谢谢!
I'm wondering how does the 8 come from, thanks!
推荐答案
的sizeof(S1)
是在内存中的数组的大小,在您的案件20个字符每一个都1字节等于20。
sizeof(s1)
is the size of the array in memory, in your case 20 chars each being 1 byte equals 20.
的sizeof(S)
是指针的大小。在不同的机器也可以是不同的大小。在我的这4。
sizeof(s)
is the size of the pointer. On different machines it can be a different size. On mine it's 4.
要测试你的机器上的不同类型的大小,你可以只通过类型,而不是像这样的printf(%祖%祖\\ N的sizeof(字符*)的sizeof(字符[变量20));
。
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]));
.
这将打印 4
和 20
分别为32位的机器上。
It will print 4
and 20
respectively on a 32bit machine.
这篇关于怎样的sizeof()为字符*(指针变量)的工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!