"要求成员'*******'不是结构或工会"是什么意思?

What does “request for member '*******' in something not a structure or union” mean?

本文关键字:意思 是什么 成员 结构      更新时间:2023-10-16

对于这个错误的含义有一个简单的解释吗?

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




};

我在非结构或联合中收到成员"姓氏"的错误请求。。。它实际上适用于所有的打印行。。。有人能帮我做这个吗?我是C编程的新手。。。。

更改

                 printf(x.Surname[temp]);

                 printf(x[temp].Surname);

无论x是指针还是数组,都不能从中取出结构成员。

你的代码中还有其他奇怪的地方。特别是在这里:

 struct student x[2]; // this array never receives data because the other x shadows it
 for(i=1; i<3; i++){
          struct student x[i]; // this declaration shadows the earlier declaration

我的猜测是,你打算做一些更像的事情

 struct student x[2];
 for(i=0; i<2; i++){
          struct student *ptr = &x[i];

那么,使用箭头运算符->也会更有意义。

此外,这也是一个问题:

                 printf(x.Age[temp]);

即使在我们修复了对的结构访问之后

                 printf(x[temp].Age);

你不能像那样把整数传给printf。字符串可以作为格式字符串,但对于整数,必须在字符串中给出格式规范。

                 printf("%d", x[temp].Age);

好吧,这段代码的bug比廉价的汽车旅馆还多。

一个明显的错误是:

scanf("%s",&temp1);

"%s"格式字符串需要一个指向字符数组的指针,它可以将字符串(包括null字符)放在该数组中。但是您已经这样声明了temp1:char temp1,它是一个单独的字符。除非您的姓名长度为0,否则您将遇到问题。最好这样定义:

char temp1[30];

或者直接写入结构的成员并跳过strcpy:

scanf("%s", x[i].Surname);

那么你就有至少29个字符的空间。但是,如果用户希望输入超过29个字符,您仍然会遇到问题。