#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char* print_stack(){
char c[20] = "I am print_stack";
puts(c);
return c;
}
char* print_malloc(){
char* c;
char* a;
c = (char *)malloc(20);
a = strcpy(c,"I am hello world");
return a;
}
int main(void)
{
char *p;
p = print_stack(); //栈空间随着函数的结束会自动被系统释放掉
puts(p);
p = print_malloc(); //堆空间必须自己手动释放 free(p);
puts(p);
return 0;
}
显示结果:
I am print_stack
I am pri2
I am hello world
请按任意键继续. . .