函数返回指针的问题
#include <stdio.h>
#include <stdlib.h>
char* getz();
int main()
{
char *p;
p=getz();
//cout<<endl;
//cout<<p;
printf("%c\n",*(p+8));//ok
printf("%s\n",p);//wrong
system("PAUSE");
return 0;
}
char* getz()
{
char s[10];
char *ps;
int i;
ps=s;
for (i=0; i<10;i++)
{
s[i]=i+'0';
}
return ps;
}
根据网上搜出来的说话是char s[10];不是static所以返回后释放掉了,所以printf("%s\n",p);//wrong,可是为什么printf("%c\n",*(p+8));//ok