c++中的指针能使用引用吗?

来源:百度知道 编辑:UC知道 时间:2024/05/30 04:59:02
#include <stdio.h>
#include <string.h>
#include <malloc.h>
void GetMemory(char *p)
{
p = (char *)malloc(100);
}
void main(void)
{
char *str = NULL;
GetMemory(str);
strcpy(str, "hello world");
printf(str);
}
这个函数怎样使用 引用才能完成主函数的功能?

你可以这样

#include <stdio.h>
#include <string.h>
#include <malloc.h>
void GetMemory(char * &p) //加一个&就行了
{
p = (char *)malloc(100);
}
void main(void)
{
char *str = NULL;
GetMemory(str);
strcpy(str, "hello world");
printf(str);
}