void fun(char *s)
{
s = (char *)malloc(100);
cout << &(s) << endl;
strcpy(s,"i love you");
cout << s << endl;
}
int main()
{
char *str = new char[100];
strcpy(str,"you and me");
cout << &str << endl;
fun(str);
cout << &str << endl;
cout << str << endl;;
}
换成二维指针
void fun(char **s)
{
*s = (char *)malloc(100);
cout << &(*s) << endl;
strcpy(*s,"i love you");
cout << *s << endl;
}
int main()
{
char *str = (char *)malloc(100);
strcpy(str,"you and me");
cout << &str << endl;
fun(&str);
cout << &str << endl;
cout << str << endl;
}
结果成了
很明显str的内容变了,但是其指针位置还是没有改变,倒是fun中的s变了