如何将 char* 作为引用传递?
我的函数使用 malloc()
我的函数使用 malloc()
void set(char *buf)
{
buf = malloc(4*sizeof(char));
buf = "test";
}
char *str;
set(str);
puts(str);
void set(char *& buf){ buf = malloc(5); strcpy(buf, "test"); }
(函数体内buf
左侧没有*
)。这样你就不会意外地传递nullptr
给函数了。 - Alexander Malakhov