Home > database >  How can we access members of the structure ,when our struct is an array?
How can we access members of the structure ,when our struct is an array?

Time:10-21

typedef struct grades{
 char s1[DIM];
 char s2[DIM];
 int i;
 float f;

}grades;

void read(grades *s[]);
void write(grades *g[]);
int main() {
    grades v[5];
    read (&v);
    write(&v);
    return 0;
}



void read (grades *s[]){
    printf("enter the name of the student number and point");
    int i;
    for (i=0;i<5;i  ){
        scanf("%s %s %d %f",s[i]->s1,s[i]->s2,s[i]->i,s[i]->f );

    }

}

void write(grades *g[]){
    int i;
    for (i=0;i<5;i  ){
        printf("%s %s %d %f\n",g[i]->s1,g[i]->s2,(g[i]->i) 5,(g[i]->f) 5 );

    }

in this short program, I want to define a struct to get students name, surname, number and then grade. then add 5 grades to their grade and print it. when we use array of structs, should we refer to the array, when we use it with pointer?

CodePudding user response:

You declared an array of structures

grades v[5];

So in this call

write(&v);

the expression &v has the type grades ( * )[5]. This pointer type is not compatible with the functions' parameter type that is implicitly adjusted to grades **g.

You need to declare the functions like

void read(grades *s, size_t n);
void write( const grades *g, size_t n);

and to call the functions like

read( v, 5 );
write( v, 5 );

This call of scanf

scanf("%s %s %d %f",s[i]->s1,s[i]->s2, s[i]->i, s[i]->f );    

shall be rewritten at least like

scanf("%s %s %d %f",s[i].s1,s[i].s2, &s[i].i, &s[i].f );
  • Related