c - 如何将当前日期的值存储到 su.date 中?

标签 c string struct

struct s
{
    char date[100];

}su;

char *formateddate()
{
   static char result[100];
   time_t t;
   t = time(NULL);
   strftime(result, sizeof(result), "%Y-%m-%d", localtime(&t));
   return result;
}



void main()
{
    char *currentDate=formateddate();
    printf("%s",currentDate);
    //su.date=currentDate;

}

错误:赋值给数组类型的表达式。

我正在尝试返回当前日期并将其存储到 su.date 中。如何存储它并进一步使用?

最佳答案

您应该使用 strcopy 复制到数组中。

strcopy(su.date, currentDate);

关于c - 如何将当前日期的值存储到 su.date 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55281690/

相关文章:

C++ sizeof() 没有返回正确的大小

c++ - 尝试使用采用结构并设置值的函数来创建链表

c - 使用动态分配的内存初始化结构

c - 获取Solaris内核模块中的进程启动时间

c - 错误 : expected expression; use of undeclared identifier

java - 从不同语言调用库函数

c++ - 在 C++ 中复制字符串数组

python - 在 Python 2 中将 'b' 字符附加到字符串文字的等效函数是什么?

c - 尝试将文本文件数据读取到结构数组中?

c++ - recvfrom() 是否在出错时修改 src_addr?