如何将值操作为指向结构的指针

How do I manipulate the values to a pointer to a struct

这是一个根据用户输入的时间值生成事件的函数,然后使用 mktime 将值转换为 Unix Epoch,并将它们存储到文件中。我已经测试并发现在我们到达用户输入值的部分之前其他一切都运行良好似乎我没有使用指向 struct tm 的指针 event 好,所以我需要帮助做对了。

int EventCreator(){
      FILE *fptr = fopen("Events_Calendar.txt", "a+");
      struct tm *event;
      printf("Enter the values of the hour minute day month year in digits and this specific format meaning using spaces to seperate them");
      printf("\n hr min day mon yr\n>");
      scanf("%d %d %d %d %d", event->tm_hour, event->tm_min, event->tm_mday, event->tm_mon, event->tm_year);
      time_t NineteenEvent = mktime(event);
      printf("%ld", NineteenEvent);
      fprintf(fptr, "%ld\n", NineteenEvent);
      fclose(fptr); 
      }

您已经声明了一个指向 struct tm 的指针,但是您还没有为 struct tm 分配内存。我建议您将 event 设为自动变量。

示例:

void EventCreator() { // make the function `void` since you do not return anything
    FILE *fptr = fopen("Events_Calendar.txt", "a+");
    if (fptr) {                             // check that opening the file succeeds

        // `event` is now not a pointer and set `tm_isdst` to a negative value
        // to make `mktime` guess if DST is in effect:
        struct tm event = {.tm_isdst = -1};

        printf(
            "Enter the values of the hour minute day month year in digits and "
            "this specific format meaning using spaces to seperate them\n"
            "hr min day mon yr\n>");

        // %d requires an `int*` but you've supplied it with `int`.
        // (also, check that scanf succeeds)
        if (scanf("%d %d %d %d %d", &event.tm_hour, &event.tm_min,
                  &event.tm_mday, &event.tm_mon, &event.tm_year) == 5) {

            event.tm_year -= 1900; // years since 1900
            --event.tm_mon;        // 0-11
            time_t NineteenEvent = mktime(&event); // take its address here
            printf("%ld", NineteenEvent);
            fprintf(fptr, "%ld\n", NineteenEvent);
            fclose(fptr);
        }
    }
}