2
不要用数组,用new // char p
4
char p[6]; 在栈中分配空间,函数退出后就被回收 所以p虽然是个地址,但不可以返回,因为空间被回收了 不可以return p; char *p=(char*)malloc(6*sizeof(int)); 在堆中分配空间,函数返回时,空间不被回收,可以return p;
#include <stdio.h> char *x_Substr() { char p[6]={'a','b'}; printf("%x %c %c\n",p,p[0],p[1]); return p; } int main(){ char* x=x_Substr(); printf("%x %c %c\n",x,x[0],x[1]); system("pause"); return 0; } 运行结果如下: 22ff30 a b 22ff30 a b Press any key to continue . . .