#include "stdio.h"
#include "string.h"
void fun(char *s){
char t[7];
s=t;
strcpy(s, "example");
}
int main(){
char *s;
fun(s);
printf("%s",s);
return 0;
}
#include "stdio.h"
#include "string.h"
void fun(char **s)
{
*s=(char*)malloc(sizeof(char)*10);
strcpy(*s,"example");
return;
}
int main()
{
char *s;
fun(&s); /* 一定要传入s的地址 */
printf("%s\n",s); /* 可以打印 example */
free(s);
s=NULL;
return 0;
}
#include "stdio.h"
#include "string.h"
void fun(char **s)
{
//*s=(char*)malloc(sizeof(char)*10);q
char t[10]; /* *s和t指向同一片栈内存,fun退出时会自动释放t指向的栈内存 */
*s=t;
strcpy(*s,"example");
return;
}
int main()
{
char *s;
fun(&s);
printf("%s\n",s);/* s指向的栈内存在fun退出时已经释放,不会打印example */
return 0;
}
供参考。
#include "stdio.h"
#include "string.h"
void fun(char *s){
char t[7];
s=t;
strcpy(s, "example");
}
int main(){
char *s;
fun(s);
printf("%s",s);
return 0;
} A 输出结果为"example"