why a char array must end with a null character. is there any reason that I've to add the null character in every char array ?
为什么char数组必须以null字符结尾。我有没有理由在每个char数组中添加空字符?
it seems that they get treated the same way.
似乎他们得到同样的待遇。
4 个解决方案
#1
3
In C, if you have a pointer to an array, then there is not way to determine the length of that array. As @AProgrammer points out, the designers could have left it at that and forced the programmer to keep track of the length of all character arrays. However, that would have made text processing in C even harder than it already is.
在C中,如果你有一个指向数组的指针,那么就无法确定该数组的长度。正如@AProgrammer指出的那样,设计人员可以将其留在那里并迫使程序员跟踪所有字符数组的长度。但是,这会使C中的文本处理比现在更难。
Therefore the language designers settled on a convention that would allow string length to be inferred by the presence of a null character indicating the end of the string.
因此,语言设计者决定采用一种约定,该约定允许通过指示字符串结尾的空字符来推断字符串长度。
For example, consider strcpy
:
例如,考虑strcpy:
char *strcpy(char *destination, const char *source);
There is no way in C to determine the length of the array that the pointers destination
and source
point to. So, without the presence of a sentinel value to indicate the end of the string, the only other solution would have been to pass extra parameters indicating the length of the source
string.
在C中无法确定指针目标和源指向的数组的长度。因此,如果不存在指示字符串结尾的标记值,则唯一的其他解决方案是传递指示源字符串长度的额外参数。
Of course, in light of modern security considerations, string processing functions that receive buffer length parameters have been introduced. But the computing landscape looked very different at the time that the null-terminated string was invented.
当然,鉴于现代安全性考虑,已经引入了接收缓冲长度参数的字符串处理功能。但是,在发明以null结尾的字符串时,计算环境看起来非常不同。
#2
8
A char array doesn't have to be null terminated (standard library functions which don't depend on this include memcpy, memmove, strncpy -- badly named this latest one --, printf with the right format string).
char数组不必是空终止的(不依赖于它的标准库函数包括memcpy,memmove,strncpy - 这个最新命名 - ,printf具有正确的格式字符串)。
A NUL Terminated Character String (NTCS) needs by definition to be terminated by a NUL. It is the format expected by string handling utilities of the C standard library and the convention used by most C program (in C++, one usually use std::string)
根据定义,NUL终止字符串(NTCS)需要由NUL终止。它是C标准库的字符串处理实用程序和大多数C程序使用的约定所期望的格式(在C ++中,通常使用std :: string)
#3
0
In general there's no reason to do that, but you must know that every string is, by definition, a char
array with null terminator. If your char
array represents a string then if you omit the null terminator every function working with C strings will not return the correct value or will behave differently than expected.
一般来说,没有理由这样做,但是你必须知道,根据定义,每个字符串都是一个带有null终止符的char数组。如果您的char数组表示一个字符串,那么如果省略null终止符,则使用C字符串的每个函数都不会返回正确的值,或者行为与预期的不同。
However char
is a type like int
,float
or double
, so you are free to create char
array as you like, without the need for them to be null-terminated.
但是char是类型为int,float或double的类型,因此您可以根据需要*创建char数组,而无需将它们作为空终止。
#4
0
Only if you want to use it as a string. Then all C/C++ string functions will search for the null char at the end, and if it's not there, they'll continue searching and sooner or later your application will crash.
仅当您要将其用作字符串时。然后所有的C / C ++字符串函数将在最后搜索null char,如果它不存在,它们将继续搜索,迟早你的应用程序将崩溃。
If you only intend to use array chars as array of chars, never referring to them as strings, then no problem. It's exactly like an array of ints.
如果你只打算使用数组字符作为字符数组,从不将它们称为字符串,那么没问题。它就像一组整数。
#1
3
In C, if you have a pointer to an array, then there is not way to determine the length of that array. As @AProgrammer points out, the designers could have left it at that and forced the programmer to keep track of the length of all character arrays. However, that would have made text processing in C even harder than it already is.
在C中,如果你有一个指向数组的指针,那么就无法确定该数组的长度。正如@AProgrammer指出的那样,设计人员可以将其留在那里并迫使程序员跟踪所有字符数组的长度。但是,这会使C中的文本处理比现在更难。
Therefore the language designers settled on a convention that would allow string length to be inferred by the presence of a null character indicating the end of the string.
因此,语言设计者决定采用一种约定,该约定允许通过指示字符串结尾的空字符来推断字符串长度。
For example, consider strcpy
:
例如,考虑strcpy:
char *strcpy(char *destination, const char *source);
There is no way in C to determine the length of the array that the pointers destination
and source
point to. So, without the presence of a sentinel value to indicate the end of the string, the only other solution would have been to pass extra parameters indicating the length of the source
string.
在C中无法确定指针目标和源指向的数组的长度。因此,如果不存在指示字符串结尾的标记值,则唯一的其他解决方案是传递指示源字符串长度的额外参数。
Of course, in light of modern security considerations, string processing functions that receive buffer length parameters have been introduced. But the computing landscape looked very different at the time that the null-terminated string was invented.
当然,鉴于现代安全性考虑,已经引入了接收缓冲长度参数的字符串处理功能。但是,在发明以null结尾的字符串时,计算环境看起来非常不同。
#2
8
A char array doesn't have to be null terminated (standard library functions which don't depend on this include memcpy, memmove, strncpy -- badly named this latest one --, printf with the right format string).
char数组不必是空终止的(不依赖于它的标准库函数包括memcpy,memmove,strncpy - 这个最新命名 - ,printf具有正确的格式字符串)。
A NUL Terminated Character String (NTCS) needs by definition to be terminated by a NUL. It is the format expected by string handling utilities of the C standard library and the convention used by most C program (in C++, one usually use std::string)
根据定义,NUL终止字符串(NTCS)需要由NUL终止。它是C标准库的字符串处理实用程序和大多数C程序使用的约定所期望的格式(在C ++中,通常使用std :: string)
#3
0
In general there's no reason to do that, but you must know that every string is, by definition, a char
array with null terminator. If your char
array represents a string then if you omit the null terminator every function working with C strings will not return the correct value or will behave differently than expected.
一般来说,没有理由这样做,但是你必须知道,根据定义,每个字符串都是一个带有null终止符的char数组。如果您的char数组表示一个字符串,那么如果省略null终止符,则使用C字符串的每个函数都不会返回正确的值,或者行为与预期的不同。
However char
is a type like int
,float
or double
, so you are free to create char
array as you like, without the need for them to be null-terminated.
但是char是类型为int,float或double的类型,因此您可以根据需要*创建char数组,而无需将它们作为空终止。
#4
0
Only if you want to use it as a string. Then all C/C++ string functions will search for the null char at the end, and if it's not there, they'll continue searching and sooner or later your application will crash.
仅当您要将其用作字符串时。然后所有的C / C ++字符串函数将在最后搜索null char,如果它不存在,它们将继续搜索,迟早你的应用程序将崩溃。
If you only intend to use array chars as array of chars, never referring to them as strings, then no problem. It's exactly like an array of ints.
如果你只打算使用数组字符作为字符数组,从不将它们称为字符串,那么没问题。它就像一组整数。