#include <stdio.h>
char *getmonth( int n );
int main()
{
int n; char *s;
scanf("%d", &n);
s = getmonth(n);
if ( s==NULL )
printf("wrong input!\n");
else printf("%s\n", s);
return 0;
}
这个是我写的
char *getmonth( int n )
{
char a[][10]={"ee","January","February","March","April","May","June","July","August","September","October","November","December"};
if(n<1||n>12) return NULL;
return a[n];
}
变量a是一个属于函数getmonth的堆栈中的临时变量,它指向的地址,函数调用结束后,就没有意义了。可以用static来修饰,改变其存储的位置。
C++的函数中不能返回临时变量的引用或指针,你的getmonth函数返回的是临时变量a的地址,此地址在函数调用结束时就释放了,此地址内的内容是无意义的。