strcpy在string.h中,而不是在stdlib.h中。malloc在stdlib.h中。
int func(){int a[1024]; return a;} 这里的a所占用空间,在函数结束后会被自动释放,无法继续使用
int * func(){int *t; t = malloc(4096); return t;} 这里申请的4K空间就可以返回给主调函数使用
int func(){int a[1024]; return a;} 这里的a所占用空间,在函数结束后会被自动释放,无法继续使用
int * func(){int *t; t = malloc(4096); return t;} 这里申请的4K空间就可以返回给主调函数使用
int func(){static int a[1024]; return a;} 这里的a所占用的空间可以释放。
#include <stdio.H>
#include <string.H>
#include <stdlib.H>
char *GetIpArray()
{
char szIp[40] = "1234";
return szIp;
}
char *GetIpMalloc()
{
char *szIp = (char *)malloc(sizeof(char)*40);
strcpy(szIp,"1234");
return szIp;
}
void main()
{
printf("%s\n",GetIpArray());
printf("%s\n",GetIpMalloc());
free(GetIpMalloc());
}