gpt4 book ai didi

c - 对于空终止字符串,strlen 有时等于 sizeof

转载 作者:太空宇宙 更新时间:2023-11-04 07:08:59 25 4
gpt4 key购买 nike

我知道 strlen 计算直到(并排除)空字符 '\0'(或 0)为止的字符数并且 sizeof 给出了存储字符串(包括空字符)所需的空间量,但与我的代码输出混淆了。

问题:

我希望 strlen 的结果始终比 sizeof 的结果小 1,因为我的字符串以 null 结尾,但似乎只有长度为 4 和 8 的字符串,不包括 '\0'(即下面的第 3 个和第 5 个结果)。我怀疑这与在第一个、第二个和第三个结果的字符串末尾打印垃圾的原因相同。有人可以解释这种行为吗?

我阅读了这个相关问题,但我不认为这是这里发生的事情:strlen - the length of the string is sometimes increased by 1 .

代码的作用:

main 中,它创建一个整数数组 0、2、4、6 和 8。然后对于每个长度,它调用函数 make_and_print_msgs 到:

  • 创建一个该长度 + 1 的字符串(对于空字符),例如对于 4 的长度,创建字符串“aaaa\0”
  • 使用 printf 中的 %c 逐个字母打印消息
  • 使用 printf 中的 %s 将其打印为字符串
  • 找到字符串的strlen
  • 找到sizeof的字符串

输出:

i    data_length[i]
--------------------
0 0
msg intended to be:
msg printed as string: �
strlen(msg): 1
sizeof(msg): 1

1 2
msg intended to be: aa
msg printed as string: aaS
strlen(msg): 3
sizeof(msg): 3

2 4
msg intended to be: aaaa
msg printed as string: aaaa
strlen(msg): 4
sizeof(msg): 5

3 6
msg intended to be: aaaaaa
msg printed as string: aaaaaai
strlen(msg): 7
sizeof(msg): 7

4 8
msg intended to be: aaaaaaaa
msg printed as string: aaaaaaaa
strlen(msg): 8
sizeof(msg): 9

代码:

(抱歉代码有点长,这就是我在上面解释的原因。代码中的一些注释是对 Python NumPy 函数的引用。)

#include <stdio.h>
#include <math.h> /* needed for ceil */
#include <string.h> /* needed for strlen */

void make_linspace(int a[], double start, double stop, int num) {
/* Fills array a[] (in place) with linearly spaced values just like np.linspace in NumPy (Python) */
double spacing = (stop-start)/(num-1);
int i;
for (i=0; i<num; i++){
a[i] = start + i*spacing;
}
}

void make_and_print_msgs(int n_proc, int msglength)
{
/* Create a string called msg of length msglength + 1 (for the null character '\0') */
char msg[msglength+1];
int i;
printf("msg intended to be: ");
for (i=0; i<msglength; i++) {
msg[i] = 'a';
printf("%c", msg[i]);
}
msg[i+1] = '\0';

/* Print message to screen as a string and fine strlen(msg) and sizeof(msg) */
printf("\n");
printf("msg printed as string: %s\n", msg);
printf("strlen(msg): %d\n", strlen(msg));
printf("sizeof(msg): %d\n\n", sizeof(msg));

}

void main(int argc, char *argv[])
{
int n_proc = 2;

/* Create an array containing the lengths of strings to be printed (In this case, data_length should be {0, 2, 4, 6, 8} */
int start = 0;
int stop_range = 10; /* the stop value if we are using range() */
int step = 2; /* spacing between the integers in the output of range() */
int stop = stop_range - step; /* the stop value if we are using linspace() */
int npoints = (int) ceil( ((double)stop_range - (double)start) / (double)step ); /* number of elements in the list produced by range(start, stop_range, step) */

int data_length[npoints]; /* 1D array of string lengths (# of non-null chars in each str) */
make_linspace(data_length, start, stop, npoints);
int i;


/* For each length, call on make_and_print_msgs to make a string of that length (plus '\0') and then print to stdout */
printf(" i data_length[i]\n--------------------\n");
for (i=0; i<npoints; i++) {
printf("%4d %7d\n", i, data_length[i]);
make_and_print_msgs(n_proc, data_length[i]);
}
}

最佳答案

将此:msg[i+1] = '\0'; 更改为 msg[i] = '\0';

您不需要增加 i,因为它已经被前面的 for 循环 增加了。

工作ideone链接:http://ideone.com/GJO1q1

关于c - 对于空终止字符串,strlen 有时等于 sizeof,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29872687/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com