如何将此强制转换解析为不同大小警告的指针?

时间:2016-03-26 16:34:26

标签: c gcc c99

我正在尝试解决使用-std = gnuc99编译的C代码中的一些警告。

void function.. (char *argument)
{
  int hour;

  hour = (int) (struct tm *)localtime(&current_time)->tm_hour;

  if(hour < 12)
  {
      do...something...
  }
}

警告

 warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
 hour = (int) (struct tm *)localtime(&current_time)->tm_hour;
              ^

我认为这里很开心的是本地时间不是指针而且与int的大小不同?

1 个答案:

答案 0 :(得分:3)

localtime(&current_time)->tm_hour的类型为int。然后将其强制转换为struct tm *,生成警告。通常,指针和int之间的转换没有意义,可能会导致未定义的行为。

要避免此错误,请移除强制转换:

hour = localtime(&current_time)->tm_hour;