Fastidious
Fastidious

Reputation: 1329

How to resolve this cast to pointer of a different size warning?

I'm trying to troubleshoot some warnings in my C code compiled with -std=gnuc99.

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

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

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

The warning

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

What I assume is happning here is that localtime is not a pointer and it's not the same size as int?

Upvotes: 1

Views: 592

Answers (1)

M.M
M.M

Reputation: 141613

localtime(&current_time)->tm_hour has type int . You then cast this to struct tm *, producing the warning. In general, conversion between pointers and int is not meaningful and may cause undefined behaviour.

To avoid this error, remove the casts:

hour = localtime(&current_time)->tm_hour;

Upvotes: 3

Related Questions