Hi @Mohsen. Welcome to our community. 🙂
A few things you should consider:
Please edit your current post and format your C code as code by selecting it and clicking the <> button in the text editor. 😉
To your main problem: you are returning a pointer to a local (stack-allocated) buffer that only exists while get_time() is running. You can't do that. In C, you have a few options:
Declare you return variable in the function that calls the desired function (in your case, it'd be in main()) and pass it to the desired function as reference. Here's an example:
void get_time(char *currentTime) { // 3. receive the buffer as reference
snprintf(currentTime, 80, "%s", "my text"); // 4. write to the buffer
}
int main(void)
{
char currentTime[80]; // 1. declare/allocate the buffer before calling the function
get_time(_timstamp, dt, currentTime); // 2. pass a reference to the buffer
printf("current time: %s \n", currentTime); // 5. buffer contains the string set by get_time()
return 0;
}
As you will notice, I didn't complete the exercise for you. Instead, I've pointed out one way to solve it. 😉
You could also use a global variable or a dynamically-allocated buffer, but your solution depends on what you've studied. If you never studied pointers, then use a global variable. If you didn't study dynamic memory allocation, then use the pointer reference as shown.
Other important fixes in your code:
You mixed C and C++ headers and functions and this can become very messy very quickly. For example, string is different from char*. Mixing them will thrown errors. So, choose one language and stick with it. In your case, I recommend choosing C. Here's the fixes you need for that:
Replace <string> by <string.h>.
Remove <iostream>.
Remove <cstring>.
Remove the using directive.
Don't use string. Use char* instead.
Replace main() by main(void).
sprintf() is insecure and decprecated. Use snprintf() instead. The latter needs the maximum number of characters to write (use the same size you've declared the buffer with).
Surely there's more room for improvement, but I'll stop here for now. 🙂
Good luck!