C语言 字符数组的大小和字符指针的大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17260242/
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
size of character array and size of character pointer
提问by user2416871
I have a piece of C code and I don't understand how the sizeof(...)function works:
我有一段 C 代码,但我不明白该sizeof(...)函数是如何工作的:
#include <stdio.h>
int main(){
const char firstname[] = "bobby";
const char* lastname = "eraserhead";
printf("%lu\n", sizeof(firstname) + sizeof(lastname));
return 0;
}
In the above code sizeof(firstname) is 6 and sizeof(lastname) is 8.
在上面的代码中,sizeof(firstname) 是 6,sizeof(lastname) 是 8。
But bobbyis 5 characters wide and eraserheadis 11 wide. I expect 16.
但是bobby是 5 个字符宽,eraserhead11个字符宽。我期待16。
Why is sizeof behaving differently for the character array and pointer to character?
为什么 sizeof 对于字符数组和指向字符的指针的行为不同?
Can any one clarify?
任何人都可以澄清吗?
回答by alk
firstnameis a chararray carrying a trailing 0-terminator. lastnameis a pointer. On a 64bit system pointers are 8 byte wide.
firstname是一个char带有尾随0终止符的数组。lastname是一个指针。在 64 位系统上,指针为 8 字节宽。
回答by Yu Hao
sizeofan array is the size of the total array, in the case of "bobby", it's 5 characters and one trailing \0which equals 6.
sizeof一个数组是整个数组的大小,在“bobby”的情况下,它是 5 个字符,一个尾随\0等于 6。
sizeofa pointer is the size of the pointer, which is normally 4 bytes in 32-bit machine and 8 bytes in 64-bit machine.
sizeof指针是指针的大小,通常在 32 位机器中为 4 个字节,在 64 位机器中为 8 个字节。
回答by gkovacs90
The size of your first array is the size of bobby\0. \0is the terminator character, so it is 6.
您的第一个数组的大小是bobby\0. \0是终止符,所以它是 6。
The second size is the size of a pointer, which is 8 byte in your 64bit system. Its size doesn't depends on the assigned string's length.
第二个大小是指针的大小,在 64 位系统中为 8 字节。它的大小不取决于指定字符串的长度。
回答by caf
firstnameis an array of 6 chars, including the terminating '\0'character at the end of the string. That's why sizeof firstnameis 6.
firstname是一个 6 的数组chars,包括'\0'字符串末尾的终止字符。这就是为什么sizeof firstname是 6。
lastnameis a pointer to char, and will have whatever size such a pointer has on your system. Typical values are 4 and 8. The size of lastnamewill be the same no matter what it is pointing to (or even if it is pointing to nothing at all).
lastname是一个指向 的指针char,并且将具有这样的指针在您的系统上具有的任何大小。典型值是 4 和 8。lastname无论指向什么(或者甚至根本不指向任何内容), 的大小都是相同的。
回答by Leigh
firstname[]is null-terminated, which adds 1 to the length.
firstname[]以空值结尾,长度加 1。
sizeof(lastname)is giving the size of the pointerinstead of the actual value.
sizeof(lastname)给出指针的大小而不是实际值。
回答by MarcH
how the sizeof(...) function works
sizeof(...) 函数的工作原理
sizeof()looks like a function but it's not a function. A function computes something at run-time.
sizeof()看起来像一个函数,但它不是一个函数。一个函数在运行时计算一些东西。
sizeof()asks the compiler, at compile-time, how much memoryit allocates for the argument. BTW sizeof()has no idea how much of it you actually use later at run time. In other words, you've "hardcoded" the printfarguments in your example.
sizeof()在编译时询问编译器它为参数分配了多少内存。BTWsizeof()不知道你在运行时实际使用了多少。换句话说,您已经“硬编码”了printf示例中的参数。
Why is sizeof behaving differently for the character array and pointer to character?
为什么 sizeof 对于字符数组和指向字符的指针的行为不同?
A pointer rarely requires the same amount of memory as an array.
指针很少需要与数组相同的内存量。
In general, the amount of memory allocated for a pointer is different to what is allocated for its pointee.
通常,为指针分配的内存量与为其指针对象分配的内存量不同。

