为什么不影响c?

问题描述 投票:0回答:1
this insthise示例,在其中我向字符串添加了一些额外的

\0

#include <stdio.h> #include <string.h> int main(int argc, char **argv){ char str1[] = "dog"; char str2[] = "dog\0"; char str3[] = "dog\0\0"; printf("%ld %ld %ld\n", strlen(str1), strlen(str2), strlen(str3)); return 0; }

但所有这些都返回3。现在:

为什么额外的
strlen()

不会导致大小增加?
    如果返回值
  1. \0
    是相同的,它们的实际大小在内存中也相同吗?
  2. strlen()
函数将字符计数为第一个null字节,即。 因此,在这三种情况下的每一个中,都有3个字符。
c string
1个回答
0
投票
因此,如果要打印数组尺寸:

strlen
将打印4 56.

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.