Kev*_*rds 0 c c++ string sizeof char
对于以下代码:
char a[] = "Apple";
char *s[] = {"Apple"};
printf("%d %d\n", sizeof(a), sizeof(s[0]));
Run Code Online (Sandbox Code Playgroud)
输出是:
6 4
Run Code Online (Sandbox Code Playgroud)
有人能告诉我为什么sizeof()给出不同的输出?
编辑:我原本打算打字sizeof(),但输入strlen().我为此道歉.我现在已经编辑了这个声明.
sizeof(a) 是数组的大小,包含终止符以及5个可打印字符.
strlen(s[0])给出字符串的长度,不包括终止符,因为这strlen是指定要执行的操作.
UPDATE:sizeof(s[0])是指针的大小.只给出一个指向它的指针,就无法确定数组的大小.