I am basically trying to get the time with
struct timeval tv = { };
gettimeofday(&tv, NULL);
And it works. The problem is that the function that is going to accept as an argument the time is of type const char*
So i did
const char *time;
time = &tv.tv_sec;
The problem is, that tv.tv_sec is of type time_t and i need to plug it in
const char *time
to pass it on to the function that needs const char*
How do i do it? Placing an integer into a const char* I've tried some simple ways of casting it, however i am not that experienced with C
When you find yourself needing to cast a time pointer into a char pointer, that is a hint that there is something very wrong. You should take a second look at the problem.
You don't describe the other function that is accepting a const char*, or I would have a better idea of what is happening.
I suspect that you may be required to convert the time integers into a character string and pass a pointer to the beginning of that string. To do that you might want to use a function called strftime.
To format an integer as a string you should use snprintf
.
For example:
char resultString[64];
snprintf(resultString, sizeof(resultString), "Current time : %d", tv.tv_sec);
Check sprintf
or snprintf
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.