什么是localtime函数的有效参数?

anu*_*amb 3 c

我这里有一个使用localtime函数的代码.但是对于其输入参数的某些值,代码崩溃(返回空指针).我想知道它的输入允许范围.

#include <stdio.h>
#include <time.h>

int main ()
{
  time_t rawtime;
  struct tm g;
  struct tm *gp;
  __int64 tim;

  tim = 7476811632013133299LL; // I know it's a weird number but valid for time_t
  rawtime = tim / 1000LL;
  gp = localtime(&rawtime);
  printf("Pointer gp = %p\n", gp);

  g = *gp; // this crahses because gp = NULL

  return 0;
}
Run Code Online (Sandbox Code Playgroud)

那么可以说关于本地时间函数的允许输入范围呢?

Kei*_*son 5

允许范围不是C标准规定的.

引用N1570草案:

localtime函数返回指向故障时间的指针,如果指定的时间无法转换为本地时间,则返回空指针.

您应该NULL在尝试取消引用之前检查结果是否正确.