void fun(char *buf)
{
buf = (char *)malloc(8);
strncpy(buf, "abcdefgh", 7);
buf[7] = '\0';
printf("%s\n", buf);
}
int main()
{
char *str = NULL;
fun(str);
//printf("====\n");
printf("=> %s\n", str);
if(str)
free(str);
}
void fun(char *buf) //该入参需要在函数内部修改,需要专递指针的指针
{
buf = (char *)malloc(8);
strncpy(buf, "abcdefgh", 7);
buf[7] = '\0';
printf("%s\n", buf);
}
int main()
{
char *str = NULL;
fun(str);
//printf("====\n");
printf("=> %s\n", str);
if(str)
free(str);
}
//请参考
void fun(char **buf) //该入参需要在函数内部修改,需要专递指针的指针
{
*buf = (char *)malloc(8);
strncpy(*buf, "abcdefgh", 7);
(*buf)[7] = '\0';
printf("%s\n", *buf);
}
int main()
{
char *str = NULL;
fun(&str);
//printf("====\n");
printf("=> %s\n", str);
if(str)
free(str);
}
C和C++完整教程:https://blog.csdn.net/it_xiangqiang/category_10581430.html
C和C++算法完整教程:https://blog.csdn.net/it_xiangqiang/category_10768339.html