c++ - What does “request for member '*******' in something not a structure or union” mean? -
is there easy explanation error means?
#include <stdio.h> #include <string.h> struct student { char surname[30]; char name[30]; int age; char address[10]; }; int main(){ int i; char temp1; char temp2; int temp3;; char temp4; struct student x[2]; for(i=1; i<3; i++){ struct student x[i]; printf(" surname of student %s:", i); scanf("%s",&temp1); printf(" other names of student %s:", i); scanf("%s",&temp2); printf(" age of student %s:", i); scanf("%s",&temp2); printf(" address of student %s:", i); scanf("%s",&temp3); strcpy(x->surname,&temp1); strcpy(x->name,&temp2); //x[i].surname=temp1; //x[i].name=temp2; x[i].age=temp3; //x[i].address=temp4; strcpy(x->address,&temp4); } int temp; if (x[1].age > x[2].age){ temp = 1; printf(x.surname[temp]); printf(x.name[temp]); printf(x.age[temp]); printf(x.address[temp]); } else if(x[1].age < x[2].age){ temp = 2; printf(x.surname[temp]); printf(x.name[temp]); printf(x.age[temp]); printf(x.address[temp]); } else{ printf(x.surname[1]); printf(x.name[1]); printf(x.age[1]); printf(x.address[1]); printf(x.surname[2]); printf(x.name[2]); printf(x.age[2]); printf(x.address[2]); } return 0; };
im getting error request member `surname' in not structure or union... print lines... can please me this? im new c programming....
change
printf(x.surname[temp]);
to
printf(x[temp].surname);
whether x
pointer or array, can't take struct member out of it.
there's other weirdness in code. particularly here:
struct student x[2]; // array never receives data because other x shadows for(i=1; i<3; i++){ struct student x[i]; // declaration shadows earlier declaration
my guess intended more like
struct student x[2]; for(i=0; i<2; i++){ struct student *ptr = &x[i];
then usage of arrow operator ->
make more sense, too.
also, problem:
printf(x.age[temp]);
even after fix struct access to
printf(x[temp].age);
you can't pass integer printf that. string can serve format string, integer must give format specification in string.
printf("%d", x[temp].age);
Comments
Post a Comment