gpt4 book ai didi

c - 结构中空数组的 strlen() 不为 0

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

我是 C 的新手,我不理解这种行为。在打印这个空数组的长度时,我得到 3 而不是 0。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct entry entry;

struct entry{
char arr[16];
};

int main(){
entry a;
printf("%d\n",strlen(a.arr));
return 0;
}

我哪里不明白?

最佳答案

entry a; 语句没有初始化结构,因此它的值可能是垃圾。因此,无法保证其任何成员的 strlen 会返回任何合理的信息。事实上,它甚至可能使程序崩溃,或者 worse .

关于c - 结构中空数组的 strlen() 不为 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36673377/

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