简体   繁体   中英

C array pointers struct retrieving the info

so this is the QI got :

Write a program that contains the following type definitions:

  1. A structure Student that contains the following members:
    • ID of the student
    • Type of grade (0 for a character grade and 1 for an int grade)
    • Grade of the student (it could be a char grade such as 'B' or an int grade such as 84)
  2. A union StuGrade that contains the following members:
    • charGrade
    • intGrade

Your program should create an array of pointers to structure Student of size 5 and fill it from user. It should then print students' information who takes grade A.

I am pretty sure my problem is in the last segment

expected :

Enter ID: 111
0 for char grade or 1 for int grade: 0
Enter a char grade: A
Enter ID: 112
0 for char grade or 1 for int grade: 0
Enter a char grade: C
Enter ID: 113
0 for char grade or 1 for int grade: 1
Enter an int grade: 84
Enter ID: 114
0 for char grade or 1 for int grade: 1
Enter an int grade: 99
Enter ID: 115
0 for char grade or 1 for int grade: 0
Enter a char grade: A
***Student(s) who take grade 'A' is/are***
ID: 111
Grade: A
ID: 114
Grade: 99
ID: 115
Grade: A

my output :

Enter ID: 111
0 for char grade or 1 for int grade:0
Enter a char grade:A
Enter ID: 112
0 for char grade or 1 for int grade:0
Enter a char grade:C
Enter ID: 113
0 for char grade or 1 for int grade:1
Enter an int grade:84
Enter ID: 114
0 for char grade or 1 for int grade:1
Enter an int grade:99
Enter ID: 115
0 for char grade or 1 for int grade:0
Enter a char grade:A
***Student(s) who take grade 'A' is/are***
ID : 115
Grade: A
ID : 115
Grade: A
ID : 115
Grade: A
ID : 115
Grade: A
ID : 115
Grade: A

Code:

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

struct StuGrade{

    int intGrade;
    char charGrade[1];

};

struct Student{

    char ID[20];
    char typeG[1];

    struct StuGrade Grade;

};

int main(){

    int i = 0;
    struct Student std[5] ;

    struct Student *ptr = NULL ;

    ptr = std ;


    for (i = 0; i < 5; i++) {

        printf("Enter ID: ");
        scanf("%s", ptr->ID);

        printf("0 for char grade or 1 for int grade:");
        scanf("%s", ptr->typeG);

        if(strcmp(ptr->typeG,"0") == 0){

            printf("Enter a char grade:");
            scanf("%s", ptr->Grade.charGrade);

        }else{

            printf("Enter an int grade:");
            scanf("%d", &ptr->Grade.intGrade);

        } 
    }

    i =0;   
    printf("***Student(s) who take grade 'A' is/are***");


    for (i = 0; i < 5; i++) {

        if(strcmp(ptr->Grade.charGrade,"A") == 0){

            printf("\nID : %s",ptr->ID);
            printf("\nGrade: %s",ptr->Grade.charGrade);

        }

        if(ptr->Grade.intGrade >= 90){  

            printf("\nID : %s",ptr->ID);
            printf("\nGrade: %d",ptr->Grade.intGrade);
        }

    }




    return 0;
}




for (i = 0; i < 5; i++) {

    if(strcmp(ptr->Grade.charGrade,"A") == 0){

        printf("\nID : %s",ptr->ID);
        printf("\nGrade: %s",ptr->Grade.charGrade);

    }

    if(ptr->Grade.intGrade >= 90){  

        printf("\nID : %s",ptr->ID);
        printf("\nGrade: %d",ptr->Grade.intGrade);
    }

}

The instructions say

create an array of pointers to structure Student

struct Student std[5] ;

is an array of structures, not an array of pointers. An array of pointers looks like:

struct Student* std[5] ;

Then you need to allocate memory for each Student structure.

for (i = 0; i < 5; i++) {
    std[i] = malloc(sizeof(*std[i]));
    ptr = std[i];
    ...
}

Another problem is that char charGrade[1]; is not big enough to hold a string with the grade. A string needs an additional character for the null terminator, so a 1-character string needs to be declared char charGrade[2]; . The same goes for typeG (although I'm not sure why you're using a string for this in the first place, it looks like it should be an int ).

The full corrected program is:

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

struct StuGrade{
    int intGrade;
    char charGrade[2];
};

struct Student{
    char ID[20];
    char typeG[2];
    struct StuGrade Grade;
};

int main(){
    int i = 0;
    struct Student* std[5] ;

    for (i = 0; i < 5; i++) {
        std[i] = malloc(sizeof(*std[i]));
        ptr = std[i];
        printf("Enter ID: ");
        scanf("%s", ptr->ID);

        printf("0 for char grade or 1 for int grade:");
        scanf("%s", ptr->typeG);

        if(strcmp(ptr->typeG,"0") == 0){
            printf("Enter a char grade:");
            scanf("%s", ptr->Grade.charGrade);
        }else{
            printf("Enter an int grade:");
            scanf("%d", &ptr->Grade.intGrade);
        } 
    }

    i =0;   
    printf("***Student(s) who take grade 'A' is/are***");

    for (i = 0; i < 5; i++) {
        ptr = std[i];
        if(strcmp(ptr->Grade.charGrade,"A") == 0){
            printf("\nID : %s",ptr->ID);
            printf("\nGrade: %s",ptr->Grade.charGrade);
        }
        if(ptr->Grade.intGrade >= 90){  
            printf("\nID : %s",ptr->ID);
            printf("\nGrade: %d",ptr->Grade.intGrade);
        }
    }

    return 0;
}

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