我正在尝试增加ID,但是由于某些原因,设置了第一个ID后,它会不断重置

时间:2019-05-07 02:32:56

标签: c pointers struct

  struct Classes
  {      //struct specific for classes
         int class_id;
         char class_title[30];
  };
  void add_class()
  {
         //Adding class
         int i, n;
         struct Classes *addClass;
         printf("How many classes are you adding? ");
         scanf("%d", &n);

   addClass = (struct Classes*) malloc(n * sizeof(struct Classes*));
   addClass->class_id = 1;

   for(i = 0; i < n; i++)
   {
            printf("Enter class titles\n");
            scanf(" %s ", (addClass+i)->class_title);
            (addClass+i)->class_id += 1; // Supposed to increment class ID
   }
   for(i = 0; i < n; i++)
   {
            printf("Class title is: %s Class ID is: %d\n", (addClass+i)->class_title, (addClass+i)->class_id);
   }
    void free(void *addclass);
   }

这是发生了什么 您要添加几类? 2 输入课程标题 几何数学 输入课程标题 类的标题是:Geom类的标识是:3 班级名称是:Math班级ID是:0

0 个答案:

没有答案