#include <stdio.h>
#include <string.h>
void TestFun(char a[100])
{
printf("before addr:%p\n", a); //打印a的地址
//a = "very good";
//错误。不能直接将字符串赋值给字符数组,a的地址改变了。估计是系统为a开辟了新的内存空间。"very good"的生存期是从进程开始到进程结束
//a[0] = "very good"; //错误。a的地址不会改变,可以改变a[0]的数值
strcpy(a, "very well");
//正确
/*
a[0] = 'v';
//正确。逐个赋值
a[1] = 'e';
a[2] = 'r';
a[3] = 'y';
a[4] = ' ';
a[5] = 'g';
a[6] = 'o';
a[7] = 'o';
a[8] = 'd';
a[9] = '\0';
*/
printf("after addr:%p\n", a); //打印a的地址
printf("%s\n", a);
}
int main(void)
{
char TestStr1[60] = {"abchij"};
char TestStr2[] = {"abchij"};
char TestStr3[100] = {65,66,67};
//strcat(TestStr1 , TestStr2);
printf("
main before TestStr3:%s\n", TestStr3);
TestFun(TestStr3);
printf("
main after TestStr3:%s\n", TestStr3);
getchar();
return 1;
}
在vs2010中的运行结果:
main before TestStr3:ABC
before addr:003EF7A4
after addr:003EF7A4
very well
main after TestStr3:very well
总结:
可以用{ printf("after addr:%p\n", a); //打印a的地址 }来打印一个指针、或数组名的地址。