我有下面的代碼,它將來自用戶的字符串輸入存儲在多維數組中。然後打印出第二個元素。在多維數組中存儲和檢索字符串
main()
{
// Array to store 10 strings, 20 characters long.
char strStorage[10][20];
printf("\nEnter how many strings: ");
scanf("%d" , &num);
fflush(stdin);
for (count = 0 ; count < num ; count++)
{
printf("Enter a string: ");
gets(strStorage[count]);
fflush(stdin);
}
printf("%s", strStorage[2]);
最後一行輸出垃圾。用戶輸入在垃圾內不可見,因此無論我的元素訪問是錯誤還是存儲錯誤。任何人都可以幫助我解決問題是什麼?
在此先感謝...
如果用戶輸入超過19個字符會怎麼樣? http://stackoverflow.com/questions/1694036/why-is-the-gets-function-is-dangerous-why-should-not-be-used – hmjd 2013-02-13 12:28:29
'fflush(stdin);'是未定義的行爲! – 2013-02-13 12:28:43
你的樣本輸入是什麼? – Rohan 2013-02-13 12:29:00