C 编程、结构体和多维数组

标签 c arrays multidimensional-array 2d

给我做了一个我似乎无法理解的练习。我几乎完成了我的作业,但我被困在这个功能上。

限制: 只能有 10 个唯一的学生 ID。 有5个学科领域的研究。 一个学生只能选修 2 门科目。

我的 struct.h 看起来像这样:

typedef struct student_info{
  int student_id;
  int course_id[2];
  }student;

在main.c中

student info[10];

在 func.c 中

假设我提示用户输入学生 ID。

printf("Enter Student ID. ");
scanf("%d", &info->student[count_stud]->student_id;

用户输入123

然后提示用户输入类(class) ID。

printf("Enter Course ID. ");
scanf("%d", &info->student->course_id[count_cour];

用户输入101

我的问题在于打印出特定的student_id 和学生正在学习的类(class)。还使用 for 循环我找不到找到重复项的方法。我可以找到用户最后输入的 ID,但是当我输入之前 2 个输入中的 ID 时,它会传递我的 if else 语句。

感谢任何帮助。

最佳答案

student info[10];

此处,info10 名学生数组,因此您必须使用索引来读取它。

for(int student_count = 0; student_count < 10; student_count ++)
{    
    printf("Enter Course ID 1 for student %d. ",student_count+1);
    scanf("%d", &info[student_count].course_id[0]);

    printf("Enter Course ID 2 for student %d. ",student_count+1);
    scanf("%d", &info[student_count].course_id[1]);
}

关于C 编程、结构体和多维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22057987/

相关文章:

c - 在 C 中使用 strdup。这里出了什么问题

c - 将整个数组传递给函数时获取垃圾值

python - NumPy 数组的阈值像素索引

java - 将大数据导入二维数组

C++ 堆栈和二维数组

c - 在 Bison 中使用字 rune 字作为终端

Javascript 'associative' 数组疯狂

javascript - Angular 4 : List doesn't update on replacement of item in backing array

php - mysql中如何存储多行多列数组

python - 从 Vala 到 Python 再返回