总所周知指针作为函数参数传递的时候 传递的是指针的拷贝(指针也是变量) 这里提供四种指针的传递方法 改到实际的指针。
#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;
}