方法一:
此处必须使用静态变量,不然调用函数随着调用结束释放了内存空间,以至于即使返回了正确的地址内容也被清空了。而静态变量则是随着主函数结束而结束与调用函数无关。
char *getmonth( int n ){
static char month[12][10]={ //静态数组
"January","February","March","April","May","June",
"July","August","September","October","November","December"
};
char *p=NULL;
if(n>0&&n<=12)
p=month[n-1];
return p;
}
方法二:
char *getmonth(int n)
{
char *getmonth[13] = { "0", "January", "February", "March", "April", "May",
"June", "July", "August", "September", "October", "November", "December" };
if (n > 0 && n < 13)
return getmonth[n];//返回对应字符串首地址
else
return 0;
}