C 语言结构体
subscripted value is neither array nor pointer nor vector //这是报错信息#include <stdio.h>
struct Student
{
long long int ID;
int D;
int C;
}student; //我这里声明了一个结构体,在①那里声明了一个结构体数组,为什么在自己定义的函数中用不了,那怎么解决呢?大佬求指点
void classified(int N,int H,int L);
void order(int *a,int n);
int main(void)
{
int N,L,H;
scanf("%d %d %d",&N,&L,&H);
struct Student student; //①
int i;
for(i=0;i<N;i++)
{
scanf("%lld %d %d",&student.ID,&student.D,&student.C);
}
classified(N,H,L);
}
void classified(int N,int H,int L)
{
int i,j,k,l,o,sum;
j=k=l=o=0;
int a,b,c,d;
for(i=0;i<N;i++)
{
if(student.D>=H && student.C>=H)
{
a=i;
}
else if(student.D>=H &&student.C>=L)
{
b=i;
}
else if(student.D>=L && student.C>=L && student.D>student.C)
{
c=i;
}
else if(student.D>=L && student.C>=L)
{
d=i;
}
}
printf("%d\n",j+k+l+o-4);
order(a,j-1);
order(b,k-1);
order(c,l-1);
order(d,o-1);
}
void order(int *a,int n)
{
struct Student temp;
int i,j;
for(i=0;i<n;i++)
{
for(j=i+1;j<n-i;j++)
{
if(student].D +student].C <student].D+student].C)
{
temp=student];
student]=student];
student]=temp;
}
}
}
for(i=0;i<n;i++)
{
printf("%lld %d %d\n",student.ID,student.D,student.C );
}
}
(一)你的 student 是结构体还是列表?俩者不同
(二)你的列表是局部还是全局?在你的 classified、order 函数里,student 是什么东西?从哪里来?这些必须告诉编译器啊。 #include <stdio.h>
struct Student
{
long long int ID;
int D;
int C;
}; // 取个student别名为毛?你后面用了吗?
void classified(struct Student student[], int N,int H,int L); // 传入student
void order(struct Student student[], int *a,int n);
int main(void)
{
int N,L,H;
scanf("%d %d %d",&N,&L,&H);
struct Student student; // 不想用全局变量,函数中传参过去
int i;
for(i=0;i<N;i++)
{
scanf("%lld %d %d",&student.ID,&student.D,&student.C);
}
classified(student, N,H,L); // 传入student
}
void classified(struct Student student[],int N,int H,int L)
{
int i,j,k,l,o,sum;
j=k=l=o=0;
int a,b,c,d;
for(i=0;i<N;i++)
{
if(student.D>=H && student.C>=H)
{
a=i;
}
else if(student.D>=H &&student.C>=L)
{
b=i;
}
else if(student.D>=L && student.C>=L && student.D>student.C) // 是student 不是 student
{
c=i;
}
else if(student.D>=L && student.C>=L)
{
d=i;
}
}
printf("%d\n",j+k+l+o-4);
// 传入student
order(student, a,j-1);
order(student, b,k-1);
order(student, c,l-1);
order(student, d,o-1);
}
void order(struct Student student[],int *a,int n)
{
struct Student temp;
int i,j;
for(i=0;i<n;i++)
{
for(j=i+1;j<n-i;j++)
{
if(student].D +student].C <student].D+student].C)
{
temp=student];
student]=student];
student]=temp;
}
}
}
for(i=0;i<n;i++)
{
printf("%lld %d %d\n",student.ID,student.D,student.C );
}
}
页:
[1]