简体   繁体   中英

Accessing a struct within struct

I have been working on a student directory which keeps track of a student's information (Name, ID, address, course name, etc.) using a list type struct and an array of structs, without using dynamic memory allocation. Below are my three structs and a function prototype:

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

The problem is that when I try to access the struct 'courseList' within the struct 'studentInfo' in my addContact function I get a few compiler errors:

25 19 [Warning] declaration does not declare anything [enabled by default]

In function 'main':

46 2 [Warning] incompatible implicit declaration of built-in function 'strcpy' [enabled by default]

In function 'addContact':

145 59 [Error] 'studentType' has no member named 'courseList'

147 59 [Error] 'studentType' has no member named 'courseList'

Here is the function defination:

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

I have tried searching the internet for hours for a solution and could not find one relevant to my problem and hope you guys can show me where I went wrong and help me fix thi compile error.

Also here is my main function just in case it might help (Ignore the function calls in the other cases in the switch because I took them out for the time being):

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

Your struct studentInfo does not contain a struct courseList member, thus the error. For a member declaration, you would need to assign a variable name (eg struct courseList theList; ). Your statement struct courseList does not declare a member by itself. Instead it is probably interpreted as a forward declaration.

Your compiler should give you a corresponding warning, such as this from gcc 4.8:

warning: declaration does not declare anything

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM