3年前。

This question was migrated from Software Engineering Stack Exchange because it can be answered on Stack Overflow. Migrated
我正在尝试解决用-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;
              ^

我假设这里发生的是localtime不是指针,它的大小与int不同?

最佳答案

localtime(&current_time)->tm_hour具有类型int。然后将其转换为struct tm *,生成警告。通常,指针与int之间的转换没有意义,可能会导致未定义的行为。
要避免此错误,请删除转换:

hour = localtime(&current_time)->tm_hour;

关于c - 如何解决此转换为不同大小的警告的指针?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36243019/

10-16 04:40