访问struct中的结构

时间:2014-07-27 20:10:54

标签: c database function data-structures struct

我一直在研究学生目录,该目录使用列表类型结构和结构数组跟踪学生的信息(姓名,ID,地址,课程名称等),而不使用动态内存分配。下面是我的三个结构和一个函数原型:

   struct listType
   {
        studentType studentList[100];  //Array of Struct studentType
        int listLength;                //Keep track of the number of contacts in the     Directory
   }studentDir;

   typedef struct studentInfo
   {
        char firstName[100];
        char lastName[100];
        char ID[100];
        char address[50];
        char phoneNumber[15];
        char birthDate[15];
        char startDate[15];
        char endDate[15];
        char GPA[10];

        struct courseList;  
    }studentType;

    struct courseList
    {
        char courseName[100];
        char creditHours[100];
        char grade[10];
    };

    void addStudent(struct listType dir);

问题在于,当我尝试访问struct' courseList'在结构中学生信息'在我的addContact函数中,我得到了一些编译器错误:

25 19 [警告]声明未声明任何[默认启用]

在功能' main':

46 2 [警告]内置函数的不兼容隐式声明' strcpy' [默认启用]

在功能' addContact':

145 59 [错误]' studentType'没有名为' courseList'

的成员

147 59 [错误]' studentType'没有名为' courseList'

的成员

这是函数定义:

    void addContact(struct listType dir)
    {
        studentDir.listLength++;

        printf("\nEnter the first name of the contact: ");
        scanf("%s", studentDir.studentList[studentDir.listLength].firstName);

        printf("\nEnter the last name of the contact: ");
        scanf("%s", studentDir.studentList[studentDir.listLength].lastName);

        printf("\nEnter the ID of the contact: ");
        scanf("%s", studentDir.studentList[studentDir.listLength].ID);

        printf("\nEnter the address of the contact: ");
        scanf("%s", studentDir.studentList[studentDir.listLength].address);

        printf("\nEnter the phone number of the contact (XXX-XXX-XXXX): ");
        scanf("%s", studentDir.studentList[studentDir.listLength].phoneNumber);

        printf("\nEnter the birthdate of the contact(MM/DD/YYYY): ");
        scanf("%s", studentDir.studentList[studentDir.listLength].birthDate);

        printf("\nEnter the start date of the contact: ");
        scanf("%s", studentDir.studentList[studentDir.listLength].startDate);

        printf("\nEnter the end date of the contact: ");
        scanf("%s", studentDir.studentList[studentDir.listLength].endDate);

        printf("\nEnter the GPA of the contact: ");
        scanf("%s", studentDir.studentList[studentDir.listLength].GPA);

        printf("\nEnter the course name of the contact: ");
        scanf("%s", studentDir.studentList[studentDir.listLength].courseList.courseName);

        printf("\nEnter the Enter the credit hours for this course: ");
        scanf("%s", studentDir.studentList[studentDir.listLength].courseList.creditHours);

        printf("\nEnter the Enter the grade for this course: ");
        scanf("%s", studentDir.studentList[studentDir.listLength].courseList.grade;         
    }

我已经尝试在网上搜索了几个小时的解决方案,找不到与我的问题相关的内容,希望你们能告诉我哪里出错了,并帮我修复编译错误。

这里也是我的主要功能,以防它可能有所帮助(在交换机的其他情况下忽略函数调用,因为我暂时将它们取出):

    int main()
    {
        int menuSelect;

        do
        {
            printf("\n================================*\n");
            printf("*   [1]Add Sudent                 *\n");
            printf("*   [2]Search Student by name     *\n");
            printf("*   [3]Search Student by ID       *\n");
            printf("*   [4]Display all Student info   *\n");    
            printf("*   (5)Quit                       *\n");
            printf("*=================================*\n");

            printf("Select an option number from the menu: ");
            scanf("%d", &menuSelect);

            switch(menuSelect)
            {
                case 1:

                    addStudent(studentDir);                 
                    break;

                case 2:

                    searchName(studentDir);
                    break;

                case 3:

                    searchID(studentDir);
                    break;

                case 4:

                    display(studentDir);
                    break;

                case 5:

                    printf("\n\t\t\t\tThank you for using the Directory!");
                    system("pause");
                    break;

            }
        }while(menuSelect != 5);    
        return 0; 
    }

1 个答案:

答案 0 :(得分:2)

您的struct studentInfo不包含struct courseList成员,因此错误。对于成员声明,​​您需要指定变量名称(例如struct courseList theList;)。您的陈述struct courseList本身并未声明成员。相反,它可能被解释为前向声明。

你的编译器应该给你一个相应的警告,比如gcc 4.8:

warning: declaration does not declare anything