【发布时间】:2013-08-23 21:18:29
【问题描述】:
这个错误的含义有简单的解释吗?
#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 编程新手....
【问题讨论】:
-
x是一个数组,而不是一个指针。 -
x 是一个数组,而不是顶级 结构 变量(因此:
x.Surname[temp]会阻塞你的编译器)。我敢打赌,“对成员的请求......在某种非结构中”现在更有意义了。 -
这段代码中的问题比您寻求帮助的要多。例如,
printf(" Surname of Student %s:", i);期望打印字符串,而不是 int。for(i=1; i<3; i++){在边界外访问您的数组。数组索引从 0 开始。 -
您有两个
x声明,一个是由 2 个struct student对象组成的数组,另一个是由istruct student对象组成的可变长度数组。 -
@chris 原始数组被隐式转换为指向数组第一个元素的指针。