總所周知指針作為函數參數傳遞的時候 傳遞的是指針的拷貝(指針也是變量) 這里提供四種指針的傳遞方法 改到實際的指針。
#include <stdio.h>
#include <memory>
#include <iostream>
using namespace std;
void test1(char **string)
{printf("string未操作之前的的指針%p\n",string);*string = "hello world";printf("string未操作之后的的指針%p\n",string);
}
char *test(char *string)
{string = "hello world";return string;
}
void test2(char *&string)
{string = "hello world";}bool test3(shared_ptr<int> &ptr)
{if(ptr)return true;ptr=make_shared<int>(10);return false;
}
int main3()
{shared_ptr<int>p;cout<<test3(p);cout<<test3(p)<<endl;cout<<*p;getchar();return 0;
}int main()
{char *str = NULL;test2(str);printf("str=%s\n",str);getchar();return 0;
}