#include <stdio.h>
#include <stdlib.h>
#define year 4 //定义大学年份为4年
typedef //学生 结构体
struct Student{
char name[10]; //存储学生姓名
int id;
int yw[4];
int sx[4];
int yy[4];
} student[10],temp; //声明 学生数组 和temp
int getStuSum(){ //获取学生个数-1
int i=0;
for(i;i<sizeof(student)/sizeof(student[0]); i++){
if( strcmp(student[i].id ,0) ){
return i;
}
}
}
expected expression before ‘struct’ 为什么一直这样子报错,怎么样修改都没办法
- 写回答
- 好问题 0 提建议
- 关注问题
- 邀请回答
-
5条回答 默认 最新
火花怪怪 2023-06-14 16:03关注在代码中,结构体的定义缺少一个名称,只有一个typedef关键字,需要在结构体定义前加上结构体名称。另外,在getStuSum函数中,对结构体中的id成员使用了strcmp函数,但是该成员是int类型,不能直接作为strcmp函数的参数,应该改为比较是否等于0。修改后的代码如下:
#include <stdio.h> #include <stdlib.h> #include <string.h> #define year 4 //定义大学年份为4年 typedef struct Student{ //添加结构体名称 char name[10]; //存储学生姓名 int id; int yw[4]; int sx[4]; int yy[4]; } Student; //修改结构体名称 Student student[10], temp; //声明 学生数组 和temp int getStuSum() { //获取学生个数-1 int i = 0; for(i; i < sizeof(student) / sizeof(student[0]); i++) { if(student[i].id == 0) { //比较是否等于0 return i; } } } int main() { return 0; }还需要注意,该程序中的id成员应该是int类型,而不是char类型,因为学生的学号通常是数字。另外,由于数组下标从0开始,getStuSum函数返回的应该是学生个数,而不是学生个数-1。
本回答被题主选为最佳回答 , 对您是否有帮助呢?评论 打赏 举报解决 1无用