C 字符串与数组名
char* getmemory(void){
char p[]= " hello world";
return p;
}
void test(void){
char *str=NULL;
str=getmemory(); printf(str);
}
请问运行Test 函数会有什么样的结果? 输出乱码
“hello world”是存储在.text段, 但 char p[] = "hello world";是定义的数组p,并初始化为 p[12] = {'h','e','l','l','o',' ',......,'\0'};
所以return p; 是返回的数组名,一个指向数组0元素的指针;