只是为了测试一些代码,我用 C 编写了以下程序:
int main(int argc, char *argv[])
{
char *a = "hello ";
char *b = "there";
char *c = malloc(100);
strcat(c, a);
strcat(c, b);
printf("length of a is %d\n", strlen(a));
printf("length of concatenated string is %d\n", strlen(c));
return 0;
}
这个的输出是:
length of a is 6
length of concatenated string is 11
现在我没有做 malloc,而是做了以下事情(其余代码相同):
char c[100];
输出变为:
length of a is 6
length of concatenated string is 12
我不明白为什么在 char 数组上使用指针会改变连接字符串的大小。