[問題] *_*請問一個結構陣列的問題..

看板C_and_CPP (C/C++)作者 (uglyman)時間16年前 (2009/12/30 11:43), 編輯推噓3(302)
留言5則, 4人參與, 最新討論串1/1
以下程式執行過程沒有問題,可是執行結束後,會跳出一個警告訊息.. Run-Time Check Failure #2 -Stack around the variable 'data' was corrupted. 我用的是Visual C++ 2008 Express的版本.. 謝謝!! ============================================================================ #include <iostream> //載入的標頭檔 using namespace std; //定義使用std命名空間 struct student { //結構student的定義宣告 char name[20],age[20],grade[20]; }; int main(void) { int i; //char data[3]; student data[3]; for(i=1;i<4;i++) { //結構變數的宣告,並設定初值 cout << "請輸入第 " << i << " 位學生資料: " << endl; cout << "請輸入姓名: "; cin >> data[i].name; cout << "請輸入年齡: "; cin >> data[i].age; cout << "請輸入成績: "; cin >> data[i].grade; cout << endl; } cout << "=======================================================" << endl; //顯示結構變數之各成員值 for(i=1;i<4;i++) { cout << "第 " << i << " 位學生資料: " << endl; cout << "姓名= " << data[i].name << endl; cout << "年齡= " << data[i].age << endl; cout << "成績= " << data[i].grade << endl; cout << endl; } system("PAUSE"); //暫停程式執行 return 0; //程式正常結束,傳回0 } -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.133.74.124

12/30 11:47, , 1F
i=1;i<4;i++ 就爆啦, 這樣子 i 用到的是 1~3 喔
12/30 11:47, 1F

12/30 11:47, , 2F
宣告 data[3] 只能用到 data[0], data[1], data[2]
12/30 11:47, 2F

12/30 12:35, , 3F
恩 就是樓上說得的那樣 只能用0~2
12/30 12:35, 3F

12/30 15:02, , 4F
嗯!可以了....謝謝...^^
12/30 15:02, 4F

12/30 16:36, , 5F
我還在想說為什麼會從1開始@@
12/30 16:36, 5F
文章代碼(AID): #1BEinieT (C_and_CPP)
文章代碼(AID): #1BEinieT (C_and_CPP)