二维指针 与 一维指针

来源:互联网 发布:爱淘宝1元口令 编辑:程序博客网 时间:2024/05/01 02:33
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变了