我的助手函式 getInput() 將資料讀入陣列串列,直到輸入結束,在那里他們將讀取員工 ID、允許的休假總數和到目前為止休假的天數。它應該回傳通過指標變數 n 讀取的記錄數。但是,當我嘗試取消參考指標時,程式將關閉,我不確定為什么。先感謝您
我的代碼:
#include <stdio.h>
#include <stdlib.h>
#define SIZE 80
typedef struct {
int id; /* staff identifier */
int totalLeave; /* the total number of days of leave allowed */
int leaveTaken; /* the number of days of leave taken so far */
}leaveRecord;
// Function Prototypes
void getInput(leaveRecord list[ ], int *n);
int mayTakeLeave(leaveRecord list[ ], int id, int leave, int n);
void printList(leaveRecord list[ ], int n);
int main(){
int choice, ID, LEAVE, leaveApproval;
int recordsRead = 0;
int *ptr = recordsRead;
leaveRecord list[SIZE];
do{
printf("Please Select one of the following Options:\n");
printf("1: getInput()\n");
printf("2: mayTakeLeave()\n");
printf("3: printList()\n");
printf("4: Quit!!\n");
scanf("%d", &choice);
switch(choice){
case 1:
getInput(list, recordsRead);
printf("Temp is %d", recordsRead);
break;
case 2:
printf("Please Enter the Staff ID:\n");
scanf("%d", &ID);
printf("Please Enter the Number of Days of Leave:\n");
scanf("%d", &LEAVE);
leaveApproval = mayTakeLeave(list,ID,LEAVE, ptr);
switch(leaveApproval){
case -1:
printf("Error!! Staff Member not found!");
break;
case 0:
printf("Leave is not approved");
break;
case 1:
printf("Leave is approved");
break;
}
break;
case 3:
break;
}
}while (choice < 3);
return 0;
}
void getInput(leaveRecord list[ ], int *n){
int option = 0, temp = 0;
int userInput;
while (option == 0){
printf("Please key in the Staff Identifier:\n");
scanf("%d", &list->id);
printf("Please key in the Total Number of Days allowed:\n");
scanf("%d", &list->totalLeave);
printf("Please key in the Number of Days of Leave taken:\n");
scanf("%d", &list->leaveTaken);
printf("Please Key in 1 if you like to stop adding Records:\n");
scanf("%d", &userInput);
if(userInput == 1){
break;
}
temp = 1;
}
// Why does dereferencing a Pointer Variable kill the entire program?
*n = temp;
}
int mayTakeLeave(leaveRecord list[ ], int id, int leave, int n){
int leaveUsed = (leave list->leaveTaken);
for(int i = 0; i < n; i = 1){
if(list->id == id){
if((leaveUsed < list->totalLeave) || (leaveUsed == list->totalLeave)){
return 1;
}
else{
return 0;
}
}
else{
return -1;
}
}
}
void printList(leaveRecord list[ ], int n){
for(int i = 0; i < n; i = 1){
printf(list);
}
}
uj5u.com熱心網友回復:
void getInput(leaveRecord list[ ], int *n);
這里,在 getInput 函式中 n 是一個整數型別的指標變數,它需要整數變數的地址。但是在這里, getInput(list, recordsRead)您只是發送讀取的記錄值。
您必須發送記錄讀取的地址。
getInput(list, &recordsRead)
同樣在函式 printList 中,您使用了錯誤的語法。列印輸出(串列);做這個 :
printf("%d",list[i]);
或者
printf("%d",*(list i));
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/342865.html
