对于以null结尾的字符串,strlen有时等于sizeof

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

题:

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

我读了这个相关的问题,但我不认为这就是这里发生的事情: strlen – 字符串的长度有时会增加1 。

代码的作用:

main ,它创建一个整数make_and_print_msgs和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  #include  /* needed for ceil */ #include  /* 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'; to msg[i] = '\0';

您不需要递增i因为它已经被前一个for loop递增。

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

在你的代码中, sizeof(msg)总是等于msglength+1因为你声明为char msg[msglength+1];

strlen(msg)始终计数直到遇到第一个'\0' 。 因此,在您的代码中,它有时是msglength+1 ,有时是msglength具体取决于未初始化的msg的初始内容。