在 C 中,是否可以创建不带 '\0(null)' 的字符串?
In C, Is it possible to create a string without '\0(null)'?
我的意思是,据我所知,当 'null value([=28=])' 出现时,字符串会 'end'。
而且很多函数都与字符串 return 值有关,一旦它在字符串中遇到 'null value' 就立即转义。
但是,如果字符串中没有空值怎么办?
甚至可以创建这样的字符串吗?
喜欢,是否可以创建一个数组
char cArray[100] ={};
并在没有 'null' 的情况下填写所有索引?
如果是这样,“指示根本没有空值的字符串结尾”的最佳方法是什么?
如果里面没有null,它甚至'a string'吗?
But, what if there's no null value in string?
那就不是字符串了。 A "string" is defined by 7.1.1 Definitions of terms, pararaph 1 在(草案)C11 标准中:
A string is a contiguous sequence of characters terminated by and including the first null character. The term multibyte string is sometimes used instead to emphasize special processing given to multibyte characters contained in the string or to avoid confusion with a wide string. A pointer to a string is a pointer to its initial (lowest addressed) character. The length of a string is the number of bytes preceding the null character and the value of a string is the sequence of the values of the contained characters, in order.
没有那个“空字符”,数组不是“字符串”。
句号。
你完全可以拥有一个没有空终止符的 char 数组(毕竟它就像一个 int 数组),但它不是一个字符串(根据定义)并且你将无法使用所有标准 C与字符串相关的函数,因为它们都依赖于终止符的存在。
最后,您必须将字符数组视为通用数组。
Is it even possible to create such string?
您可以拥有不含零元素的 char 数组。这些不是字符串。
Like, is it possible to create an array
char cArray[100]; // the original had the illegal initializer = {};
and fill all the indexes out without 'null'?.
是的,这是完全合理的 (memset(cArray, '*', 100);
)。但是 cArray
(或其任何部分)将不是字符串。
If so, what's the best way to 'indicate the end of string that doesn't have null value at all'?
如果您想使用没有指示符的字节序列,您需要单独计数。
char NotAString[6] = "abcdef";
size_t n = 6;
// remove the 'f' from NotAstring
n = 5;
// remove the 'b'
memmove(NotAString + 1, NotAString + 2, 3);
n -= 1;
// the first `n` (4) bytes of NotAString are now 'a', 'c', 'd', and 'e'
// don't care about bytes 5 and 6
Is it even 'a string' if there's no null in it?
没有.
这实际上取决于您的最终目标。
But, what if there's no null value in string?
任何采用 char*
参数(字符串)的标准 C 库函数都需要空终止符。如果您不在字符串的末尾添加一个,无论函数采用您未终止的字符串,都只会继续读取超出字符串末尾的部分,即 UB。
Is it even possible to create such string?
嗯,是的,但它不会很有用。您可以分别存储长度和字符串并执行如下操作:
char myString[100] = { ... } // unterminated string
size_t myStringLength = 100;
for (int i = 0; i < myStringLength; i++)
{
// do stuff
}
但这只适用于您自己的实现,同样,没有标准的 C 库函数支持它。
is it possible to create an array and fill all the indexes out without 'null'?.
再一次,是的。数组只是一个数组,而这个数组实际上只是一个“数字”数组。正如 Jabberwocky 提到的,字符串是通过在末尾有一个空终止符来定义的。
If so, what's the best way to 'indicate the end of string that doesn't have null value at all'?
With the standard C library functions, there is no way to do this.
总结:
Is it even 'a string' if there's no null in it?
没有:)
在 C 中使用非空终止文本数据是很常见的。您只需要注意您的术语,因为这样的数据在形式上不是“字符串”。
如果您有一些不以空字符结尾的文本数据,您显然必须以其他方式跟踪其长度。最常见的方法是保留一个包含长度的单独变量(通常称为 n
、size
、sz
、length
或 len
)。
对于大多数对字符串起作用的操作,有相应的方法来处理非空终止文本数据。这是一个 table:
operation
conventional string
counted text
read from file
fgets
fread
, read
copy
strcpy
memcpy
compute length
strlen
n/a
write to file
fputs
fwrite
, write
您可以使用技巧创建包含非空终止“字符串”的数组:
char nonstr[5] = "hello";
通常,当您使用字符串文字初始化 char
数组时,C 编译器会选择大小,并包括 space 作为空终止符。也就是说,当你写
char str[] = "hello";
数组的大小为 6。但是当您指定一个明确的大小时,并且当它“太小”恰好为 1 时,您会得到一个非 null 终止的字符串。总结:
char error[3] = "hello"; /* error: array too small */
char nonstr[5] = "hello"; /* special case: no null */
char string[6] = "hello"; /* normal case: right size, including null */
char extra[10] = "hello"; /* extra space (could contain larger string later) */
char default[] = "hello"; /* default case: compiler picks size 6 */
C 中的字符串是以零结尾的字符数组。
But, what if there's no null value in string?
那么就不是字符串了,而是原始数据数组
Is it even possible to create such string?
不,因为那样它就不是字符串了。
is it possible to create an array and fill all the indexes out without 'null'?.
当然可以。字符数组不一定是字符串。
Is it even 'a string' if there's no null in it?
没有
同时勾选
我的意思是,据我所知,当 'null value([=28=])' 出现时,字符串会 'end'。
而且很多函数都与字符串 return 值有关,一旦它在字符串中遇到 'null value' 就立即转义。
但是,如果字符串中没有空值怎么办?
甚至可以创建这样的字符串吗?
喜欢,是否可以创建一个数组
char cArray[100] ={};
并在没有 'null' 的情况下填写所有索引?
如果是这样,“指示根本没有空值的字符串结尾”的最佳方法是什么?
如果里面没有null,它甚至'a string'吗?
But, what if there's no null value in string?
那就不是字符串了。 A "string" is defined by 7.1.1 Definitions of terms, pararaph 1 在(草案)C11 标准中:
A string is a contiguous sequence of characters terminated by and including the first null character. The term multibyte string is sometimes used instead to emphasize special processing given to multibyte characters contained in the string or to avoid confusion with a wide string. A pointer to a string is a pointer to its initial (lowest addressed) character. The length of a string is the number of bytes preceding the null character and the value of a string is the sequence of the values of the contained characters, in order.
没有那个“空字符”,数组不是“字符串”。
句号。
你完全可以拥有一个没有空终止符的 char 数组(毕竟它就像一个 int 数组),但它不是一个字符串(根据定义)并且你将无法使用所有标准 C与字符串相关的函数,因为它们都依赖于终止符的存在。
最后,您必须将字符数组视为通用数组。
Is it even possible to create such string?
您可以拥有不含零元素的 char 数组。这些不是字符串。
Like, is it possible to create an array
char cArray[100]; // the original had the illegal initializer = {};
and fill all the indexes out without 'null'?.
是的,这是完全合理的 (memset(cArray, '*', 100);
)。但是 cArray
(或其任何部分)将不是字符串。
If so, what's the best way to 'indicate the end of string that doesn't have null value at all'?
如果您想使用没有指示符的字节序列,您需要单独计数。
char NotAString[6] = "abcdef";
size_t n = 6;
// remove the 'f' from NotAstring
n = 5;
// remove the 'b'
memmove(NotAString + 1, NotAString + 2, 3);
n -= 1;
// the first `n` (4) bytes of NotAString are now 'a', 'c', 'd', and 'e'
// don't care about bytes 5 and 6
Is it even 'a string' if there's no null in it?
没有.
这实际上取决于您的最终目标。
But, what if there's no null value in string?
任何采用 char*
参数(字符串)的标准 C 库函数都需要空终止符。如果您不在字符串的末尾添加一个,无论函数采用您未终止的字符串,都只会继续读取超出字符串末尾的部分,即 UB。
Is it even possible to create such string?
嗯,是的,但它不会很有用。您可以分别存储长度和字符串并执行如下操作:
char myString[100] = { ... } // unterminated string
size_t myStringLength = 100;
for (int i = 0; i < myStringLength; i++)
{
// do stuff
}
但这只适用于您自己的实现,同样,没有标准的 C 库函数支持它。
is it possible to create an array and fill all the indexes out without 'null'?.
再一次,是的。数组只是一个数组,而这个数组实际上只是一个“数字”数组。正如 Jabberwocky 提到的,字符串是通过在末尾有一个空终止符来定义的。
If so, what's the best way to 'indicate the end of string that doesn't have null value at all'? With the standard C library functions, there is no way to do this.
总结:
Is it even 'a string' if there's no null in it?
没有:)
在 C 中使用非空终止文本数据是很常见的。您只需要注意您的术语,因为这样的数据在形式上不是“字符串”。
如果您有一些不以空字符结尾的文本数据,您显然必须以其他方式跟踪其长度。最常见的方法是保留一个包含长度的单独变量(通常称为 n
、size
、sz
、length
或 len
)。
对于大多数对字符串起作用的操作,有相应的方法来处理非空终止文本数据。这是一个 table:
operation | conventional string | counted text |
---|---|---|
read from file | fgets |
fread , read |
copy | strcpy |
memcpy |
compute length | strlen |
n/a |
write to file | fputs |
fwrite , write |
您可以使用技巧创建包含非空终止“字符串”的数组:
char nonstr[5] = "hello";
通常,当您使用字符串文字初始化 char
数组时,C 编译器会选择大小,并包括 space 作为空终止符。也就是说,当你写
char str[] = "hello";
数组的大小为 6。但是当您指定一个明确的大小时,并且当它“太小”恰好为 1 时,您会得到一个非 null 终止的字符串。总结:
char error[3] = "hello"; /* error: array too small */
char nonstr[5] = "hello"; /* special case: no null */
char string[6] = "hello"; /* normal case: right size, including null */
char extra[10] = "hello"; /* extra space (could contain larger string later) */
char default[] = "hello"; /* default case: compiler picks size 6 */
C 中的字符串是以零结尾的字符数组。
But, what if there's no null value in string?
那么就不是字符串了,而是原始数据数组
Is it even possible to create such string?
不,因为那样它就不是字符串了。
is it possible to create an array and fill all the indexes out without 'null'?.
当然可以。字符数组不一定是字符串。
Is it even 'a string' if there's no null in it?
没有
同时勾选