Memory 在运行时向结构成员提供输入时获取分段错误

Memory 在运行时向结构成员提供输入时获取分段错误,memory,kernel,gdb,Memory,Kernel,Gdb,我写了一个小代码来获取n个学生的信息。 但是在运行程序之后,我得到了一个错误。请在下面查找代码 struct students { char name[20]; int age; int id; }student[100]; int main() { int count; int no_students; printf("Enter no of students"); scanf("%d",&no_studen

我写了一个小代码来获取n个学生的信息。 但是在运行程序之后,我得到了一个错误。请在下面查找代码

struct students
{
     char name[20];
     int age;
     int id;
}student[100];
int main()
{
      int count;
      int no_students;
      printf("Enter no of students");
      scanf("%d",&no_students);
      for (count = 1 ; count <= no_students ; count++)
      {
           printf("Enter the details for student%d\n",count);
           printf("Name:");
           scanf("%s",student[count].name);
           printf("Age:");
           scanf("%d",student[count].age);
           printf("ID:");
           scanf("%d",student[count].id);
      }
      return 0; 
 }

 root@debian:/home/renga/C_code# ./nike
 Enter no of students3
 Enter the details for student1
 Name:renga
 Age:12
 Segmentation fault

找到问题了,scanf中的错过参考运算符

        printf("Age:");
        scanf("%d",&(student[count].age));
        printf("ID:");
        scanf("%d",&(student[count].id));

这可能不是询问C语言帮助/语法问题的合适组。我建议您阅读一本优秀的C编程语言书籍,例如:

话虽如此,问题是,您试图错误地读取int数据类型age&id值。您需要将这些值读入变量的地址,如下所示

scanf("%d",&student[count].age);

它的StackOverbox问题:和数组是基于0的,所以for循环被关闭了1