我的意思是,据我所知,当出现“空值 (\0)”时,字符串将“结束”。
很多函数都与字符串返回值有关,一旦遇到字符串中的“空值”就转义。
但是,如果字符串中没有空值呢?
甚至可以创建这样的字符串吗?
比如,是否可以创建一个数组
char cArray[100] ={};
并在没有'null'的情况下填写所有索引?
如果是这样,“指示根本没有空值的字符串结尾”的最佳方法是什么?
如果其中没有空值,它甚至是“字符串”吗?
最佳答案
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?
否。
关于arrays - 在 C 中,是否可以创建没有 '\0(null)' 的字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69584069/