看板 C_and_CPP 關於我們 聯絡資訊
今天去面試的考題 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
VictorTom:置底十誡之六.... 06/30 23:47
tsaiminghan:不能回傳local的address 06/30 23:48
zlw:用static char temp[10];作弊。沒有標頭檔,非win沒有pause 06/30 23:54
VictorTom:用Global Variable作弊XD 07/01 00:00
softwind:static char[] 即可, 之前不是還有一題 why 不行 07/01 00:31
softwind:char new[10]; why 不能過 compiler 07/01 00:31
VictorTom:C++不行是因為new是關鍵字吧:) 07/01 00:55