Reputation: 1329
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(¤t_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(¤t_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
Reputation: 141613
localtime(¤t_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(¤t_time)->tm_hour;
Upvotes: 3