【发布时间】:2017-01-18 02:20:41
【问题描述】:
首先,道歉:C 的新手,而且总体上在编码方面相当绿色。
问题:尝试使用 strftime() 在 C 中创建时间戳。 这有效:
#include <time.h>
...
char timestamp[14];
...[code to define timestruct]
strftime(timestamp,15,"%Y%m%d%H%M%S", timestruct);
但是 this 给出了分段错误:
#include <time.h>
...
char *timestamp;
...[code to define timestruct]
strftime(timestamp,15,"%Y%m%d%H%M%S", timestruct);
显然,我有一个可行的解决方案,所以基本上一切都很好。但是为什么以timestamp为指针的版本不起作用呢?
提前致谢。
【问题讨论】:
-
尝试首先在第二种情况下分配时间戳。也就是说,做
char *timestamp = malloc(14)。 -
第二种情况下调用
strftime(timestamp,15,...时指针timestamp的值是多少? -
@gowrath 知道了,谢谢。
-
@chux 例如“20160909204853”加上可能是终止
\0。