Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 什么是“;会员申请'*******';在某种非结构或联合的事物中”;什么意思?_C++_C_Struct - Fatal编程技术网

C++ 什么是“;会员申请'*******';在某种非结构或联合的事物中”;什么意思?

C++ 什么是“;会员申请'*******';在某种非结构或联合的事物中”;什么意思?,c++,c,struct,C++,C,Struct,这个错误意味着什么,有没有简单的解释 #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; i

这个错误意味着什么,有没有简单的解释

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









};
#包括
#包括
结构学生{
查氏[30];
字符名[30];
智力年龄;
字符地址[10];
};
int main(){
int i;
字符temp1;
字符temp2;
int temp3;;
字符temp4;
结构学生x[2];
对于(i=1;iSurname,&temp1);
strcpy(x->Name和temp2);
//x[i]。姓氏=temp1;
//x[i].Name=temp2;
x[i].年龄=temp3;
//x[i]。地址=temp4;
strcpy(x->Address和temp4);
}
内部温度;
如果(x[1]。年龄>x[2]。年龄){
温度=1;
printf(x.姓氏[temp]);
printf(x.Name[temp]);
printf(x.Age[temp]);
printf(x.地址[temp]);
}
else if(x[1]。年龄
我收到了一个非结构或联合中成员“姓氏”的错误请求。。。实际上,所有的打印行都是这样的。。。有人能帮我吗?我是C编程新手

变化

                 printf(x.Surname[temp]);

无论
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
即使在我们修复了对

                 printf(x[temp].Age);
不能像这样将整数传递给printf。字符串可以用作格式字符串,但对于整数,必须在字符串中指定格式

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

无论
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
即使在我们修复了对

                 printf(x[temp].Age);
不能像这样将整数传递给printf。字符串可以用作格式字符串,但对于整数,必须在字符串中指定格式

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

好的,这个代码比便宜的汽车旅馆有更多的bug

一个明显的错误是:

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

char temp1[30];
或者直接写入结构的成员并跳过strcpy

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

然后,您最多可以有29个字符的空间。但是如果用户想要输入超过29个字符,您仍然会遇到问题。

好的,这个代码比便宜的汽车旅馆有更多的bug

一个明显的错误是:

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

char temp1[30];
或者直接写入结构的成员并跳过strcpy:

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

然后,您最多可以有29个字符的空间。但是,如果用户希望输入超过29个字符,您仍然会遇到问题。

x
是一个数组,而不是指针。x是一个数组,而不是顶级结构变量(因此:
x.姓氏[temp]
将阻塞编译器)。我打赌“请求成员…在非结构的东西中”现在更有意义。这段代码中的问题比您请求帮助的问题更多。例如,
printf(“学生%s的姓氏:,i”)
希望打印字符串,而不是int。
的(i=1;iYou对
x
有两个声明,一个作为2个
struct student
对象的数组,另一个作为
i
struct student
对象的可变长度数组。@chris原始数组隐式转换为指向数组第一个元素的指针。
x
是数组,而不是指针。x是数组,n一个顶级结构变量(因此:
x.lasname[temp]
将阻塞您的编译器)。我打赌“在非结构中请求成员”现在更有意义。此代码中的问题比您请求帮助的问题更多。例如,
printf(“学生的姓氏:”,I);
希望打印字符串,而不是int。
的(i=1;iYou对
x
有两个声明,一个作为2个
struct student
对象的数组,另一个作为
i
struct student
对象的可变长度数组。@chris原始数组隐式转换为指向数组第一个元素的指针。