how to get timestamp in c

后端 未结 5 1086
情话喂你
情话喂你 2021-01-19 10:53

I want to get timestamp for my log in c. i have written a function to get timestamp. But when i return the variable i m getting different value.

My code:

<         


        
5条回答
  •  难免孤独
    2021-01-19 11:20

    You are returning a pointer to a stack variable and therefore it is not valid to use after the function returns:

     char buffer[16];
    

    Will be allocated on the stack in the function. When you return the stack is cleaned up and buffer is no longer valid. With minimal changes this is probably a better function signature:

    void get_timestamp( char *buffer, size_t buffLen  )
    

    The assumption being that you have properly allocated space for buffer before calling get_timestamp.

提交回复
热议问题