arrays - 在 C 中,是否可以创建没有 '\0(null)' 的字符串?

我的意思是,据我所知,当出现“空值 (\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/

相关文章:

javascript - 如何更改 react js中的默认端口

c - 如何在 yocto 中编译一个基本的 c 文件

haskell - 在 'let' 表达式中使用 'if'

r - 在 R 中转换日期格式

macos - Applescript 制作新文件夹

python - 如何获取和使用 Dropbox API 的刷新 token (Python 3.x

.net - 如何在 .NET 中替换“

python - 有没有一种很好的方法来拆分(可能)长的字符串而不用 Python 中的单词拆分?

flutter - 为什么 Visual Studio Code 中的代码文本没有颜色?

perl - 如何在 Perl 中将本地时间转换为 Unix 时间戳?