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 c开发者_JAVA百科asting 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);
Directly useable C functions are:
ctime
(input is of typetime_t
)asctime
(input is of typestruct tm
)
If you need control over format use:
strftime
(input is of typestruct tm
)
The linked pages contain examples of use. The string has to be preallocated.
Check sprintf
or snprintf
.
精彩评论