Question

I'm trying to call a structure to a function but I'm getting the error: 'course' undeclared The error is in the hw3func program. I typedef'd my structure and malloc'd space for it inside of main, but I don't know what I need to make my function prototype or my function call to recognize the struct? Thank you for the help in advance!!

hw3.c:

#include <stdio.h>
#include <stdlib.h>
#include "hw3func.c"

int main(void)
{
    coursestruct *course = malloc(1*sizeof(coursestruct));
    studentstruct *student = malloc(1*sizeof(studentstruct));
    display();
    menu();

    return 0;
}

hw3.h:

#include <stdio.h>
#include <stdlib.h>

/*Start of prototypes*/
void display(void);
void menu(void);
void newcourse(int coursetotal, coursestruct *course);
void newstudent(int studenttotal);
/*End of prototypes*/



/*Start of initial struct declares*/
//  coursestruct *course = malloc(1 * sizeof(coursestruct));
//  studentstruct *student = malloc(1 * sizeof(studentstruct));
/*End of initial struct declares*/



/*Start of variables*/
    int coursetotal=0;
    int studenttotal=0;
/*End of variables*/

hw3struct.h:

typedef struct{
    char name[50];
    int id[4];
}coursestruct;

typedef struct{
    char name[20];
    int id[8];
}studentstruct;

hw3func.c

#include <stdio.h>
#include <stdlib.h>
#include "hw3struct.h"
#include "hw3.h"

void display(void)
{}

void menu(void)
{
    int loop=0; /*Loop for the menu*/

    while(loop==0)
    {
        int option;

        printf("\n\n\nWelcome to the grade book! Select your option below.\n");
        printf("1=  Add a new course.\n");
        printf("2=  Add a new student.\n");
        printf("3=  Add a student to a course.\n");
        printf("4=  Add grades for a student in a course.\n");
        printf("5=  Print a list of all grades for a student in a course.\n");
        printf("6=  Print a list of all students in a course.\n");
        printf("7=  Compute the average for a student in a course.\n");
        printf("8=  Print a list of all courses.\n");
        printf("9=  Print a list of all students.\n");
        printf("10= Compute the average for a course.\n");
        printf("11= Store grade book to a disk file.\n");
        printf("12= Load grade book from a disk file.\n");
        printf("13= Exit grade book.\n");
        scanf("%d",&option);
        printf("\n\n\n");

        if(((option>0) && (option<14)))
        {
            switch(option)
            {
                case 1:
                    newcourse(coursetotal,course);
                    break;

                case 13:
                    loop=1;
                    break;

                default:
                    break;
            }
        }

        else
        {
            printf("Please input a number between 1-13.\n");
        }
    }
}

void newcourse(int coursetotal, coursestruct *course)
{
}
Was it helpful?

Solution

You declare coursestruct *course inside main, so it is not directly accessible from other functions (like your menu() function).

You should do some reading on variable scope in C.

In this case, the proper fix is most likely to change your function declarations to something like

void menu (coursestruct *course);

Then call it from main as menu(course);. This allows the menu function access to the variable declared inside main.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top