结构体指针使用

来源:百度知道 编辑:UC知道 时间:2024/06/07 11:57:22
简化部分:
typedef struct
{
char Name[20];
int Score;
}Student;

int Insert_a_record(Student stud[],int n)
{ char a[20];Student *s; int i;
printf("Name:");
scanf("%s",*(s.Name));
printf("Score:");
scanf("%d",&*(s.Name));
n=Insert(stud,n,s);
gets(a);
printf("success!");
return n;
system("pause");
}

int Insert(Student stud[],int n,Student *s)
{ n=n+1;
strcpy(stud[n].Name,*(s.Name));
stud[n].Score=*(s.Score);
return n;
}
主要就是本人对结构体指针s中的成员Name和Score不会用,调试有
错误若干,跪求请教……急!!
试过还是不行啊~还是改的那几行报错

有个显然的错误
s是个指针吧,那么:
scanf("%s",*(s.Name));应为scanf("%s",(*s).Name);
scanf("%d",&*(s.Name)); 应为scanf("%d",&(*s).Score);
strcpy(stud[n].Name,*(s.Name)); 应为strcpy(stud[n].Name,(*s).Name);
stud[n].Score=*(s.Score);应为 stud[n].Score=(*s).Score;
//.的运算优先级高于*,所以要括起来或者你用s->Sorce这样的语法也可以。.叫结构体成员运算符,->叫指向结构体成员运算符

秩序

void Insert(Student stud[],int *n,Student *s)
{
strcpy(stud[*n].Name,s.Name);
stud[n].Score=s.Score;
++*n;
}

Insert_a_record 是干吗的?

顺便说一下
对于Student s,引用:s->Score
对于Student *p,引用:p.Score 或 (*p).Score
还有,少用点大写字母,多用小写字母