Home > Enterprise >  Why does Dereferencing a Pointer Variable in my Helper Function causes my entire program to terminat
Why does Dereferencing a Pointer Variable in my Helper Function causes my entire program to terminat

Time:10-31

My Helper Function, getInput() will read the data into the array list until end of input, where they will read in the Staff ID, total number of leave allowed and the number of days of leave taken so far. It is supposed to return the number of records read through the pointer variable n. However, when I try to dereference the pointer, the program will close and I am not sure why. Thank you in advance

My Code:

#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);
    }
}

CodePudding user response:

void getInput(leaveRecord list[ ], int *n);

Here, In getInput function n is an integer type pointer variable which wants address of integer variable. But here, getInput(list, recordsRead) you are just sending value of records read.

You have to send address of recordsRead.

getInput(list, &recordsRead)

Also in function printList you are using wrong syntax. printf(list); Do this :

printf("%d",list[i]);

or

printf("%d",*(list i));
  • Related