How to access the fields of a timeval structure

后端 未结 4 553
说谎
说谎 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条回答
  •  囚心锁ツ
    2021-01-06 08:58

    Because cur is a pointer. Use

    struct timeval cur;
    do_gettimeofday(&cur);
    

    In Linux, do_gettimeofday() requires that the user pre-allocate the space. Do NOT just pass a pointer that is not pointing to anything! You could use malloc(), but your best bet is just to pass the address of something on the stack.

提交回复
热议问题