为什么没有'\ 0'的strlen工作正常?

时间:2017-04-26 04:35:23

标签: c string strlen

作为标题,我在c中使用char *有一些问题。例如,如果我写这个

with open('result_id_with_total.csv', 'w') as newfile:
    header = ['KEY ID', 'GENERAL ID' , 'TOTAL GENERAL ID']
    wr = csv.writer(newfile, delimiter=',', quoting = csv.QUOTE_MINIMAL)

,输出

char *a = calloc(5, 5);
a[0] = '1';
a[1] = '1';
a[2] = '1';
a[3] = '1';
a[4] = '1';
printf("a = %s, length = %d", a, strlen(a));

为什么没有'\ 0'的strlen工作正常?有人可以帮我理解吗?

1 个答案:

答案 0 :(得分:3)

calloc(5, 5)分配和输出25个字节。您分配了前五个,但第六个仍然是'\0'