Rodbjartson Rodbjartson - 2 months ago 10
C Question

C: Incompatible pointer to integer conversion

I have a struct with dates and time.

struct datetime{
int hour;
etc...}


When I try to assign the correct hour to my own struct-hour, I get an error.

struct tm tm_struct = *localtime(time(NULL));

dt->hour = tm_struct->tm_hour;


First I get an incompatible pointer conversion error on the first line, then I get an error about using
tm_struct.tm_hour
instead of
->
.

Any help would be appreciated :)

AnT AnT
Answer

localtime expects a pointer to time_t as its argument. Instead you are passing time_t value itself, which is the root of your first problem. Typically because of this you will have to introduce an intermediate named time_t object to hold the result of time

time_t t = time(NULL);
struct tm tm_struct = *localtime(&t);

preventing you from using your original one-liner.

But by using compound literals you can still express it as a one-liner

struct tm tm_struct = *localtime(&(time_t) { time(NULL) });

if you so desire.