将typedef'd结构调用到函数中?

时间:2014-03-24 00:23:35

标签: c function struct malloc typedef

我试图将结构称为功能,但我收到错误:'当然'未申报 错误发生在hw3func程序中。我在main中键入了我的结构和malloc空间,但是我不知道我需要什么才能使我的函数原型或我的函数调用来识别结构?感谢您提前帮助!!

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)
{
}

1 个答案:

答案 0 :(得分:0)

您在coursestruct *course内声明了main,因此无法直接从其他功能(例如您的menu()功能)访问它。

你应该对C中的变量范围进行一些阅读。

在这种情况下,正确的修复最有可能将您的函数声明更改为

void menu (coursestruct *course);

然后从main将其称为menu(course);。这允许menu函数访问main内声明的变量。