C - Convert between struct timeval and uint32_t to calculate elapsed time

606 views Asked by At

I read a lot of posts on stackoverflow about endianness for network.
For example, if I do something like this :

struct timeval start_t
gettimeofday(&start_t, NULL);

Now , how can I be sure this kind of cast will work ?

uint32_t receivedTime = someGetterSomewhere();
struct timeval result = (struct timeval) receivedTime;
struct timeval diffTime;
timersub(&start_t,&result,&diffTime);


// do stuff like timersub, etc to get ms elapsed time 

int calculatedTime =  (int) floor( ((diffTime.tv_sec) * 1000 + (diffTime.tv_usec) / 1000) + 0.5);

Thanks for answering

PS:

My Gcc says this is not allowed

error: aggregate value used where an integer was expected
         pkt_set_timestamp(emptyPacket, (const uint32_t) start_t);
0

There are 0 answers