- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我知道在 C 中字符串以 NUL '\0'
字节终止。但是,我无法弄清楚为什么字符串文字中的 0
的行为与在堆栈上创建的 char 数组中的 0
的行为不同。检查文字中的 NUL 终止符时,数组中间的零不会被如此处理。
例如:
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
int main()
{
/* here, one would expect strlen to evaluate to 2 */
char *confusion = "11001";
size_t len = strlen(confusion);
printf("length = %zu\n", len); /* why is this == 5, as opposed to 2? */
/* why is the entire segment printed here, instead of the first two bytes?*/
char *p = confusion;
while (*p != '\0')
putchar(*p++);
putchar('\n');
/* this evaluates to true ... OK */
if ((char)0 == '\0')
printf("is null\n");
/* and if we do this ... */
char s[6];
s[0] = 1;
s[1] = 1;
s[2] = 0;
s[3] = 0;
s[4] = 1;
s[5] = '\0';
len = strlen(s); /* len == 2, as expected. */
printf("length = %zu\n", len);
return 0;
}
输出:
length = 5
11001
is null
length = 2
为什么会出现这种情况?
最佳答案
变量“confusion”是指向文字字符串的字符的指针。所以内存看起来像这样
[11001\0]
因此,当您打印变量“confusion”时,它将打印所有内容,直到第一个空字符(由\0 表示)。
11001 中的零不为空,它们是文字零,因为它用双引号引起来。
但是,在变量 's' 的 char 数组赋值中,您将十进制值 0 赋给字符变量。当您这样做时,ASCII 十进制值 0(即 NULL 字符的 ASCII 字符值)将被分配给它。所以字符数组看起来就像在内存中
[happyface, happyface, NULL]
ASCII 字符 happyface 的 ASCII 十进制值为 1。因此,当您打印时,它将打印直到第一个 NULL 为止的所有内容,因此strlen 为 2。
这里的技巧是理解当十进制值分配给字符变量时真正分配给它的是什么。
试试这个代码:
#include <stdio.h>
int
main(void)
{
char c = 0;
printf( "%c\n", c ); //Prints the ASCII character which is NULL.
printf( "%d\n", c ); //Prints the decimal value.
return 0;
}
关于c - C 中的 NUL 字符和静态字符数组/字符串文字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43383444/
关闭。这个问题需要details or clarity .它目前不接受答案。 想改进这个问题吗? 通过 editing this post 添加细节并澄清问题. 关闭 7 年前。 Improve t
我有一个 const char* 指向十六进制格式的数据,我需要找到我正在检查的数据的长度 NUL-terminator 但是当\x00 出现时它检测到它作为 NUL 终止符返回不正确的长度。 我该如
如果 nul 永远不能成为 Windows 中的一个文件,那么如果使用对 nul 没有相同规则的操作系统在所有目录中创建一个名为“nul”的文件会怎么样。例如,如果在 CMD 中执行“echo 123
我知道 strlen() 不计算 NUL 终止字符。我真的知道这是事实。因此,这个问题并不是要问为什么 strlen() 可能“大概”不返回正确的字符串长度,这个问题已经在 StackOverfl
虽然 CStr 通常用于 FFI,但我正在读取 &[u8],它以 NUL 结尾并确保是有效的 UTF-8,所以没有需要检查。 但是 NUL 终止符不一定在切片的末尾。将此作为 &str 获取的好方法是
虽然 CStr 通常用于 FFI,但我正在读取 &[u8],它以 NUL 结尾并确保是有效的 UTF-8,所以没有需要检查。 但是 NUL 终止符不一定在切片的末尾。将此作为 &str 获取的好方法是
我正在使用 Java BufferedWriter 逐行写入文件。问题是,当我写完文件时,它在整个文件中显示“nul”。 我不认为问题出在我正在写的内容上,因为我用System.out.print仔细
我正在使用 R 使用 fread 读取一个文件,该文件包含许多列和行。该文件如下所示: 1_17118 1_18353 1_21882 1_21955 1_22054 Ind0001 -1
c89 海合会 (海湾合作委员会) 4.6.3 你好, 我在 *search = '\0' 上得到一个堆栈转储;我认为可以用 nul 终止一个字符串。 char *ptr_name = "hello@
这个问题在这里已经有了答案: NUL undeclared- first use in this function (4 个答案) 关闭 8 年前。 char word[5]={"fayed"};
我曾经使用以下函数写入很多不同的文件 using (FileStream fs = new FileStream(Settings.PsLog, FileMode.Truncate, System.S
我正在编写一个下载器,它将 url 拆分为多个部分并使用线程下载,可能我不会使用“join”,因为 join = 无法流式传输(如果所有线程未完成,则无法写入文件) 但是问题是 f.seek 和 wr
这是我服务器端的方法: void send_err(int socket_fd, char *msg) { /// To send an error message (`ERR`).
我需要创建一个将标准错误流重定向到某个文件的新进程。创建子进程的代码没有可用的控制台,因此在某些情况下 GetStdHandle(any) 将返回 0。子进程将出于某种原因尝试复制其所有标准 IO 句
我的 Notepad++ 中有这样的字符 当我尝试复制整行时,我实际上正在复制所有内容,直到“NUL”: File:1 我想做的就是替换那些空的,什么都没有,这样我就可以复制我的整行。也许有任何关键字
我正在通过 Java 中的 UDP 接收带有字节数组的数据包。我知道最大可能的字节大小,但我不知道当前接收的大小。 如果我从这个字节数组创建一个 String 实例,那么该字符串在有用的有效负载之后将
来自 the web我知道 C 包含 NUL 关键字。 编译时出现错误 NUL undeclared first use in this function' 我的代码片段: for (;;) {
#include #include int main() { char x[] = "Happy birthday to You"; char y[25]; char z[
我看到了here如果它不是 NULL 终止的,则不可能使用 strlen 找出 (unsigned char *) 字符串长度,因为 strlen 函数将遍历字符串但不会找到任何 '\0',因此运行-
很难说出这里问的是什么。这个问题是含糊的、模糊的、不完整的、过于宽泛的或修辞性的,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开它,visit the help center 。 已关
我是一名优秀的程序员,十分优秀!