Engineering & Technology
มหาวิทยาลัย
เคลียร์แล้ว

問題1:請問我能讓使用者輸入全班的人數後,再把使用者輸入的值設成陣列的大小嗎(第18行)
問題2:因為我有呼叫3函數,但這三個函數都有接收結構變數,我想把結構陣列設成全域都可使用,但因為不知道全班的人數,結構陣列的大小就無法得知,有什麼方式可以解決這個問題呢?
因為照片數量限制而沒辦法拍到完整的程式,希望能有高手能幫我解答^_^

1 #include<stdio.h> 2 #include<stdlib.h> 3 struct data 4日 { 5 char name [20]; 6 int grade; 7 }; 8 typedef struct data DATA; 9 void highest(DATA, int); 10 void failed(DATA, int); 11 void average (DATA, int); 12 int main() 13日 1 14 int num; 15 printf("請輸入全班的人數\n"); 16 scanf("%d",&num); 17 DATA student[num]; 18 資源福領語紀錄 除回復結果 長小化 訊息 ser\AppData\Local\Temp\ccUASCHd.o #1.cpp:(.text+0x1ef): undefined reference to 'highest(data, int)' ser\AppData\Local\Temp\ccUASCHd. 11.cpp:(.text+0x231): undefined reference to failed(data, int)' ser\AppData\Local\Temp\ccUASCHd.o 1x1.cpp:(.text+0x273): undefined reference to average(data, int)' iser\Desktop\collect2.exe [Error] Id returned 1 exit status 31 反白字数: 0 约行数: 67 客長度:1335 插入模式 完成解析(在了0.016 秒) 日
Desktop\新文件1.cpp - Dev-C++ 5.11 搜言(S) 检0 專案(P) 執行) 工具] AStyle 視窗() 求助(日) 口回贈多「血 TDM-GCC 4.9.2 64-bit ( (globals) 除義 新文件1.cpp 19白 for(int i=0;i<num; i++){ 20 printf("請輸入%d號學生的名字”,i+1); 21 gets(student[i].name); 22 printf("請輸入%d號學生的分數\n",i+1); 23 scanf("%d", student[i].grade); 24 fflush(stdin); 25 } 26 highest(student[num], num); 27 failed(student [num], num); 28 average(student[num], num); 29 30 system("pause"); 31 return 0; 32"} 339 void highest (DATA student[], int num){ 34 int max, count; 35 max=student[0].grade; 36 for(int i=1;i<num; i++){ 資源槍 血通經紀錄 宗旨 国結果图長小化 訊息 (4) • C:\Users\User\AppData\Local\Temp\ccUASCHd.o C:\Users\User\AppData\Local Temp\ccUASCHd.o C:\Users\User\AppData\Local\Temp\ccUASCHd. C:\Users\User\Desktop\collect2.exe 訊息 新文件1.cpp:(.text+ Oxlet); undefined reference to highest(data, int)' 新文件1.cpp:(.text+0x231): undefined reference to falled(data int)' +1.cpp:(.text+0x273): undefined reference to 'average(data, int)' [Error] Id returned 1 exit status . 17 列数 21 反白字數, 0 行数:57 检察長度:1335 插入模式 完成解析(花了0.016 秒) o go DEV
iser\Desktop\新文件1.cpp - Dev-C++ 5.11 三日受尊S 檢視M 專案(P) 執行O 工具D AStyle 現金 求助(H) 19回 TDM-GCC 4.9.2 64-bit Release ( (globals) 別 宗旨 新文件1.cpp 36白 for(int i=1;i<num; i++){ 37白 if(student[i].grade>max){ 38 max=student[i].grade; 39 count=i; 40 } 41 i++; 42 } 43 printf("成績最高分的人是%s,是%d分" student[count].name, student[count] • grade); 44 45 } 469 void failed(DATA student[], int num) { 47 int flag=0; 48 printf(“以下列出的為不及格的人:\n"); 49日 for(int i=0;i<num; i++){ 50白 if(student[i].grade<60){ 51 printf("%s 為%d分,不及格\n", student[i].name,student[i].grade); 52 flag=1; 53 } 日通器訊息(4) 四資源檔血通经经络除這回搜尋結果 最小化 数列数检察 訊息 • C:\Users\User\AppData\Local\Temp\ccUASCHd.o 新文件1.cpp:(.text+ Oxler: undefined reference to highest(data int)' C:\Users\User\AppData\Local\Temp\ccUASCHd.o 新文件1.cpp:(.text+0x231): undefined reference to falled(data int)' C:\Users\User\AppData\Local\Temp\ccUASCHd.o 新文件1.cpp:(.text+0x273): undefined reference to average(data, int)' C:\Users\User\Desktop\collect2.exe [Error] Id returned 1 exit status 插入式 兒成解析(花了 0.016 秒) 17 21 列数 反白字: 客县E:1335 0 67 進行數: 31°C 39 DEV DA -
寫程式

คำตอบ

✨ คำตอบที่ดีที่สุด ✨

Unfortunately a process can’t allocate unspecified number of certain struct on stack.You have to use malloc to dynamically allocate memory which,of course, is less efficient compared with allocation on stack.

Jasmine

Thank you~

แสดงความคิดเห็น
PromotionBanner
ข้อสงสัยของคุณเคลียร์แล้วหรือยัง?