我是 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/