什么是无效的终止字符串?
它们与非终止终止的字符串有何不同?终止字符串的零是什么?它与空不同吗?我应该自己终止我的字符串,还是编译器会为我做到这一点?为什么需要终止终止的字符串?如何设置代码/数据来处理零终止的字符串?
How are they different from non-null-terminated strings? What is this null that terminates the string? Is it different from NULL? Should I null-terminate my strings myself, or the compiler will do it for me? Why are null-terminated strings needed? How do I set up my code/data to handle null-terminated strings?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
什么是无效的终止字符串?
在C中,“无效终止的弦”是一种重言式。 A string is, by definition, a contiguous null-终止字符的顺序(数组或数组的一部分)。其他语言可能以不同的方式解决字符串。我只是在讨论c字符串。
它们与非终止的字符串有何不同?
C中没有非终止的字符串。非终止的字符数组只是一系列字符。
这个终止字符串的空是什么?它与null有所不同吗?
“空字符”是一个零整数值的字符。 (从本质上讲,字符是小整数)。有时,尤其是在ASCII的背景下,称为NUL(单个L)。这与Null(Double L)不同,这是Null Pointer 。空字符可以写为
'\ 0'
或仅在源代码中的0
。这两种形式在C中可以互换(但在C ++中不可互换)。前者通常是首选的,因为它表现出更好的意图。我应该自己终止我的字符串,还是编译器为我做的?
如果您正在编写字符串文字,则无需在最后明确插入空字符。编译器将做到这一点。
在声明具有显式大小的数组并用字符串字面的字符字面字符初始化时,编译器将不会插入空字符。
在声明数组时,编译器不会插入空字符,并且不使用字符串字面的字符串初始化。
如果您在运行时构建字符串,从IO或程序的其他部分中的某些数据中构建字符串,则需要确保插入空终结器。例如:
标准库函数,例如
fread
和posix函数,例如read
永远不会将其参数释放。strncpy
如果有足够的空间,请添加一个空末端,因此请谨慎使用。令人困惑的是,strncat
将始终添加一个null末端。为什么需要无效的终止字符串?
标准C库中的许多功能以及第三方库中的许多功能都在字符串上运行(和 ALL 字符串,都需要被终止终止)。如果将非终止的字符数组传递给期望字符串的函数,则结果可能是未定义的。因此,如果您想与周围的世界进行互操作,则需要终止终止的字符串。如果您从未使用任何期望弦乐参数的标准图书馆或第三方功能,则可以做您想做的事。
如何设置我的代码/数据来处理零终止的字符串?
如果您打算存储最高n长的字符串,请为您的数据分配n+1个字符。未包括字符串的长度所需的字符,但它包含在存储它所需的数组的 size 中。
What are null-terminating strings?
In C, a "null-terminated string" is a tautology. A string is, by definition, a contiguous null-terminated sequence of characters (an array, or a part of an array). Other languages may address strings differently. I am only discussing C strings.
How are they different from a non-null-terminated strings?
There are no non-null-terminated strings in C. A non-null-terminated array of characters is just an array of characters.
What is this null that terminates the string? Is it different from NULL?
The "null character" is a character with the integer value of zero. (Characters are, in essence, small integers). It is sometimes, especially in the context of ASCII, referred to as NUL (single L). This is distinct from NULL (double L), which is a null pointer. The null character can be written as
'\0'
or just0
in the source code. The two forms are interchangeable in C (but not in C++). The former is usually preferred because it shows the intent better.Should I null-terminate my strings myself, or the compiler will do it for me?
If you are writing a string literal, you don't need to explicitly insert a null character in the end. The compiler will do it.
The compiler will not insert a null character when declaring an array with an explicit size and initialising it with a string literal with more characters than the array can hold.
The compiler will not insert a null character when declaring an array and not initialising it with a string literal.
If you are building a string at run-time out of some data that comes from IO or from a different part of the program, you need to make sure a null terminator is inserted. For example:
Standard library functions such as
fread
and POSIX functions such asread
never null-terminate their arguments.strncpy
will add a null-terminator if there is enough space for it, so use it with care. Confusingly,strncat
will always add a null-terminator.Why are null-terminated strings needed?
Many functions from the standard C library, and many functions from third-party libraries, operate on strings (and all strings need to be null-terminated). If you pass a non-null-terminated character array to a function that expects a string, the results are likely to be undefined. So if you want to interoperate with the world around you, you need null-terminated strings. If you never use any standard-library or third-party functions that expect string arguments, you may do what you want.
How do I set up my code/data to handle null-terminated strings?
If you plan to store strings of length up to N, allocate N+1 characters for your data. The character needed for the null terminator is not included the length of the string, but it is included in the size of the array required to store it.