繁体   English   中英

“在非结构体或联合体中请求成员'*******'”是什么意思?

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

关于此错误的含义有简单的解释吗?

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









};

我在某种不是结构或联合的情况下收到成员'Surname'的错误请求...实际上是所有打印行的信息...有人可以帮我吗? 我是C编程的新手。

更改

                 printf(x.Surname[temp]);

                 printf(x[temp].Surname);

无论x是指针还是数组,都无法从中删除struct成员。

您的代码中还有其他怪异之处。 特别是在这里:

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

好的,此代码比便宜的汽车旅馆有更多的错误。

一个明显的错误是这样的:

scanf("%s",&temp1);

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

char temp1[30];

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

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

那么您最多可以容纳29个字符。 但是,如果用户希望输入超过29个字符,您仍然会遇到问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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