作者hunter73419 (大大)
看板C_and_CPP
標題[問題] 指標回傳問題
時間Tue Jun 30 23:47:04 2009
今天去面試的考題
char* p(void) {
char temp[10];
strcpy(temp, "hello");
return temp;
}
int main(void) {
char *ptr;
ptr=p();
printf("%s", ptr);
system("pause");
return 0;
}
題意應該是要output hello 這個字
但是這段code是沒辦法的 請問錯在哪??
應該要怎麼改呢??
謝謝
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 59.117.163.127
1F:→ VictorTom:置底十誡之六.... 06/30 23:47
2F:推 tsaiminghan:不能回傳local的address 06/30 23:48
3F:推 zlw:用static char temp[10];作弊。沒有標頭檔,非win沒有pause 06/30 23:54
4F:推 VictorTom:用Global Variable作弊XD 07/01 00:00
5F:推 softwind:static char[] 即可, 之前不是還有一題 why 不行 07/01 00:31
6F:→ softwind:char new[10]; why 不能過 compiler 07/01 00:31
7F:推 VictorTom:C++不行是因為new是關鍵字吧:) 07/01 00:55