我寫了下面:字符串和字符串函數用C
#include <stdio.h>
#include <string.h>
char* getString();
char* getString(){
char str[10];
gets(str);
return str;
}
int main() {
char* s;
s=getString();
strcpy(s,"Hi");
puts(s);
return 0;
}
我知道的str
長度必須小於10少,但即使當我寫只是「嗨」,正在打印什麼。據我所知,它應該是好的。編譯器說fgets is dangerous and should not be used
。
什麼原因沒有在屏幕上打印?
你返回本地...不要做... :)我很驚訝,編譯器是不是給你一個警告。 – Mysticial 2012-07-17 06:47:58
相關:http://stackoverflow.com/questions/6441218/can-a-local-variables-memory-be-accessed-outside-its-scope – Flexo 2012-07-17 06:50:30