"要求成员'*******'不是结构或工会"是什么意思?
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;
};
我在非结构或联合中收到成员"姓氏"的错误请求。。。它实际上适用于所有的打印行。。。有人能帮我做这个吗?我是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个字符,您仍然会遇到问题。
相关文章:
- 这个语法std::class<>{}(arg1, arg2) 在C++中是什么意思?
- 标准 N3337 5.2.10 第 7 条中的C++"类型"是什么意思?
- "类模板示例<int>;"语句对 C++11 是什么意思?
- 线应该是什么意思
- "CID"在AT+HTTPPARA= "CID" ,1中是什么意思
- 表达式"b=(b-x)&x"是什么意思?
- 这个表达是什么意思?
- 此代码验证公式是什么意思?
- 跟踪日志中的T.11803()是什么意思?
- 这个额外的关键字在这个 c++ 类声明中是什么意思?
- * 和 ** 在 C++ 函数声明中是什么意思?
- _T("xyz")是什么意思?
- #define Dbg(fmt,..) (0) 是什么意思? 警告:表达式无效
- 这行代码到底是什么意思?
- 当我们说在执行程序时将操作系统的控制权传递给main()函数时,我们的意思是什么
- 这句话在c++中的意思是什么
- 这个typedef的意思是什么
- 这句话的意思是什么
- 意思是什么
- 这个句法的意思是什么?