char * test(){
return "hello";
}
char * test2(){
///Users/lanqs/Desktop/c语言学习/谭启宏-C-9/谭启宏-C-9/main.c:23:12: Address of stack memory associated with local variable 'string' returned
// char string[20] = "HELLO";
// char * string = "HELLO";
char * string = malloc(20);
*string ='b';
*(string +1) ='a';
// free(string);//(有木有这一句后面都没得输出,eee 这一句是对内存的管理不能及时清理,所以应该写在下面)
return string;//直接赋值第二个元素不会输出,因为第一个元素是'\0'
}
//test()
char *temp = test();
printf("temp = %s\n",temp);
// temp[0]='h';//崩溃
// printf("temp = %s",temp);
temp ="wrold";//指向world地址
printf("temo = %s\n",temp);
//test2()
char *temp1 = test2();
printf("temp1 = %s\n",temp1);
输出结果:
temp = hello
temo = wrold
temp1 = ba
Hello, World!
Program ended with exit code: 0