How to access the fields of a timeval structure

后端 未结 4 599
说谎
说谎 2021-01-06 08:51

I\'m trying to print the values in a struct timeval variable as follows:

int main()  
{  

    struct timeval *cur;  
    do_gettimeofday(cur);          


        
4条回答
  •  猫巷女王i
    2021-01-06 09:01

    You need to use the -> operator rather than then . operator when accessing the fields. Like so: cur->tv_sec.

    Also you need to have the timeval structure allocated. At the moment you are passing a random pointer to the function gettimeofday().

    struct timeval cur;
    gettimeofday(&cur);
    printf("%ld.%ld", cur.tv_sec, cur.tv_nsec);
    

提交回复
热议问题