C语言 空终止 const char* 的 sizeof
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4054284/
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 for a null terminated const char*
提问by hari
const char* a;
how do I make sure that string 'a' is null terminated? when a = "abcd" and I do sizeof(a), I get 4. Does that mean its not null-terminated? if it were, I would have gotten 5 ?
如何确保字符串 'a' 以空字符结尾?当 a = "abcd" 并且我执行 sizeof(a) 时,我得到 4。这是否意味着它不是以 null 结尾的?如果是这样,我会得到 5 吗?
采纳答案by T.E.D.
If you are handed a char array which may or may not have null-terminated data in it, there really isn't a good way to check. The best you can do is search for a null character up to a certian specified length (not indefinitely!). But 0 isn't exactly an unusual byte of data to find in an uninitialzed area of memory.
如果您收到一个字符数组,其中可能包含或不包含以空字符结尾的数据,那么确实没有一个好的检查方法。您能做的最好的事情是搜索一个空字符,直到指定长度(不是无限期!)。但是 0 并不是在未初始化的内存区域中可以找到的不寻常的数据字节。
This is one of the many things about C's defacto string standard that many people dislike. Finding the length of a string a client hands you is an O(n) search operation at best, and a segmentation fault at worst.
这是许多人不喜欢 C 的事实上的字符串标准的许多事情之一。查找客户端交给您的字符串的长度充其量是 O(n) 搜索操作,最坏的情况是分段错误。
Another issue of course is that arrays and pointers are interchangable. That means array_name + 2is the same as &(array_name[2]), and sizeof(a)is sizeof(char*), not the length of the array.
当然,另一个问题是数组和指针是可以互换的。这意味着array_name + 2与 相同&(array_name[2]),并且sizeof(a)是sizeof(char*),而不是数组的长度。
回答by James McNellis
sizeof(a)gives you the size of the pointer, not of the array of characters the pointer points to. It's the same as if you had said sizeof(char*).
sizeof(a)给你指针的大小,而不是指针指向的字符数组。就像你说的一样sizeof(char*)。
You need to use strlen()to compute the length of a null-terminated string (note that the length returned does notinclude the null terminator, so strlen("abcd")is 4, not 5). Or, you can initialize an array with the string literal:
您需要使用strlen()来计算空终止字符串的长度(请注意,返回的长度不包括空终止符,因此strlen("abcd")是 4,而不是 5)。或者,您可以使用字符串文字初始化数组:
char a[] = "abcd";
size_t sizeof_a = sizeof(a); // sizeof_a is 5, because 'a' is an array not a pointer
The string literal "abcd"is null terminated; all string literals are null terminated.
字符串文字"abcd"以空结尾;所有字符串文字都是空终止的。
回答by jer
You get 4because that's the size of a pointer on your system. If you want to get the length of a nul terminated string, you want the strlenfunction in the C standard library.
你得到,4因为这是你系统上一个指针的大小。如果要获取以 nul 结尾的字符串的长度,则需要strlenC 标准库中的函数。
回答by JaredPar
The problem here is that you are confusing sizeof()which is a compile time operation with the length of a string which is a runtime operation. The reason get 4 back when you run sizeof(a)is that ais a pointer and the typicalsize of a pointer in C is 4 bytes. In order to get the length of the string use strlen.
这里的问题是您混淆sizeof()了编译时操作与运行时操作的字符串长度。运行时返回 4 的原因sizeof(a)是它a是一个指针,而C 中指针的典型大小是 4 个字节。为了获得字符串的长度,请使用strlen.
For the second question, how to make sure a string is null terminated. The only way to definitively do this is to null terminate the string yourself. Given only a char*there is no way to 100% guarantee it is properly null terminated. Great care must be taken to ensure the the contract between the producer and consumer of the char*is understood as to who terminates the string.
对于第二个问题,如何确保字符串以空字符结尾。明确执行此操作的唯一方法是自己将字符串终止为 null。仅给出一个char*就无法 100% 保证它正确地以空终止。必须非常小心以确保生产者和消费者之间的合同char*被理解为谁终止字符串。
回答by aschepler
sizeof(a)is sizeof(const char*), the size of the pointer. It is not affected by the contents of a. For that, you want strlen.
sizeof(a)是sizeof(const char*),指针的大小。它不受内容的影响a。为此,您想要strlen.
Also, all double-quoted string literals like your "abcd"in source code are automatically null terminated.
此外,像"abcd"源代码中的所有双引号字符串文字都会自动以空字符结尾。
回答by semaj
sizeof(a) returns the size of the const char *a...not the size of what it is pointing to. You can use strlen(a)to gind the length of the null-terminated string and no, the result of strlen does not include the null-terminator.
sizeof(a) 返回...的大小,而const char *a不是它指向的大小。您可以使用strlen(a)gind 以空字符结尾的字符串的长度,不, strlen 的结果不包括空字符终止符。

