[問題] 請教結構指標的題目

看板C_and_CPP (C/C++)作者 (Liou)時間11年前 (2015/04/26 22:13), 11年前編輯推噓0(009)
留言9則, 2人參與, 最新討論串1/1
大家好,小弟目前在自學C語言,買書作習題發現有個題目不懂的地方 還請大家多多幫忙。(實在是想不透阿~~~) 題目:利用一個指向結構data型態的指標,來指向結構陣列student, 如下敘: struct data *ptr; ptr=student; 是以ptr指標算術運算完成下面的程式設計。 #include <stdio.h> #include <stdlib.h> #define MAX 6 int main(void) { int i,sum=0; struct data { char name[10]; int math; }student[MAX]={{"Jenny",63},{"Teresa",88}, {"Kimi Chen",57},{"Lily Kao",92}, {"David",48},{"Bird",23}}; struct data high=student[0]; for(i=0;i<MAX;i++) { if(high.math<student[i].math) { strcpy(high.name,student[i].name); high.math=student[i].math; } } printf("%s的數學成績最高是%d分\n",high.name,high.math); printf("\n以下為不及格的名單:\n"); for(i=0;i<MAX;i++) if(student[i].math<60) printf("%s不及格,成績為=%d\n",student[i].name, student[i].math); for(i=0;i<MAX;i++) sum+=student[i].math; printf("\n平均成績=%.2f\n",(float)sum/MAX); system("pause"); return 0; } ※ 編輯: elohacp4 (114.45.159.145), 04/26/2015 22:16:26

04/26 22:38, , 1F
他就是要你把所有student[i]都改成*(student+?)而已
04/26 22:38, 1F

04/27 21:33, , 2F
謝謝^_^
04/27 21:33, 2F

04/27 21:36, , 3F
:O 你不會真的打 ? 吧
04/27 21:36, 3F

04/27 22:00, , 4F
?改成[i]是這樣子對吧
04/27 22:00, 4F

04/28 08:44, , 5F
不對。你用電腦執行看結果最快。
04/28 08:44, 5F

04/28 09:40, , 6F
對阿,我改成*(student+[i])可以執行,結果也都正確阿
04/28 09:40, 6F

04/28 10:25, , 7F
這樣根本通不過編譯 那牌compiler可以讓你過的
04/28 10:25, 7F

04/28 20:13, , 8F
奇怪,怎麼又不能執行了....
04/28 20:13, 8F

04/28 20:29, , 9F
好啦改成(ptr+i)->就可以啦!!!
04/28 20:29, 9F
文章代碼(AID): #1LFFATnh (C_and_CPP)
文章代碼(AID): #1LFFATnh (C_and_CPP)