c语言写一个到指定内存空间获取时间的函数
时间: 2024-05-06 08:19:40 浏览: 13
以下是一个获取当前时间的示例函数,将当前时间存储在指定的内存空间中:
```
#include <stdio.h>
#include <time.h>
void get_current_time(void *ptr) {
time_t current_time;
struct tm * time_info;
time(¤t_time);
time_info = localtime(¤t_time);
memcpy(ptr, time_info, sizeof(struct tm));
}
int main() {
struct tm current_time;
get_current_time(¤t_time);
printf("Current time: %d-%02d-%02d %02d:%02d:%02d\n",
current_time.tm_year + 1900, current_time.tm_mon + 1, current_time.tm_mday,
current_time.tm_hour, current_time.tm_min, current_time.tm_sec);
return 0;
}
```
在此示例中,我们使用了time.h头文件中的time和localtime函数来获取当前时间,并使用memcpy函数将时间信息存储在指定的内存空间中。在主函数中,我们使用存储在内存中的时间信息来打印当前时间。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)