C struct tm&time_t
发布时间:2020-12-16 03:12:50 所属栏目:百科 来源:网络整理
导读:我在这里有一个时间阵列: struct cl{ unsigned char *buffer; time_t t = time(0); struct tm * ct = localtime(t);}; 接着: cl sadi[10]; 但是例如我在21:58得到了sadi [5],当时我在21:59得到了sadi [6]. 然后我再次检查我的sadi [].ct- tm_min是59.这是
我在这里有一个时间阵列:
struct cl{ unsigned char *buffer; time_t t = time(0); struct tm * ct = localtime(&t); }; 接着: cl sadi[10]; 但是例如我在21:58得到了sadi [5],当时我在21:59得到了sadi [6]. 解决方法
这一行:
struct tm* ct = localtime(&t); 问题是localtime(&)返回的指针是静态内部缓冲区.所以每次调用它都返回完全相同的指针值(地址).这意味着所有的数组元素都有指向同一个struct tm对象的指针. 一个解决方案是在每次调用本地时间时复制数据: struct cl { unsigned char* buffer; time_t t = time(0); struct tm ct = *localtime(&t); }; 所以现在我声明struct tm ct; (而不是指针),并使用返回的指针* localtime(& t)的取消引用值进行初始化. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |