<分区>
Possible Duplicate:
What is the difference between char s[] and char *s in C?
Difference between char *str = “…” and char str[N] = “…”?
我有一些让我感到困惑的代码。
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
char* string1 = "this is a test";
char string2[] = "this is a test";
printf("%i, %i\n", sizeof(string1), sizeof(string2));
system("PAUSE");
return 0;
}
当它输出 string1 的大小时,它打印 4,这是意料之中的,因为指针的大小是 4 个字节。但是当它打印 string2 时,它输出 15。我认为数组是一个指针,所以 string2 的大小应该与 string1 相同,对吧?那么为什么它为同一类型的数据(指针)打印出两种不同的大小呢?