字符数组指针返回错误(当前年份和时间)
Char Array Pointer Returns Incorrect (Current Year ctime)
此函数
char *uniqid()
{
static char uniqid[13];
time_t curTime = time(0);
struct tm *time = gmtime(&curTime);
//Year
uniqid[0] = '1';
uniqid[1] = '5';
uniqid[2] = 'n';
return uniqid;
}
在cout中调用时返回"15",这通常是应该的,但是当我这样做时
char *uniqid()
{
static char uniqid[13];
time_t curTime = time(0);
struct tm *time = gmtime(&curTime);
//Year
uniqid[0] = ((time->tm_year + 1900) % 100) / 10;
uniqid[1] = ((time->tm_year + 1900) % 100) % 10;
uniqid[2] = ' ';
return uniqid;
}
当在cout中调用时,它返回奇怪的图标。
'1'
和1
是不同的值
从1
得到'1'
,只需加上'0'
uniqid[0] = ((time->tm_year + 1900) % 100) / 10;
uniqid[0] += '0';
uniqid[1] = (((time->tm_year + 1900) % 100) % 10) + '0';
tm_year
为右整型。你将int类型赋值给char。这就产生了那个奇怪的图标。您需要通过添加48(表示0的ASCII码)将其转换为char。
相关文章:
- 1d 智能指针不适用于语法 (*)++
- C++为构建时间获取QDateTime的可靠方法
- 在C#中处理C++指针而不使用unsafe的最佳方法
- 为什么使用 "this" 指针调用派生成员函数?
- 从持续时间构造std::chrono::system_clock::time_point
- 函数向量_指针有不同的原型,我可以构建一个吗
- 使用指针从C++中的数组中获取最大值
- 助记符和指向成员语法的指针
- 指向(数据)成员的指针作为非类型模板参数,例如具有自动存储持续时间/无链接
- 一个循环,可以打印所有项目(无论链条多长时间) *指针
- C++ - 函数中的局部指针变量具有什么类型的存储持续时间以及它们存储在哪里?
- 使用指针在恒定时间内从集合中删除
- 如何确定指针转换将被偏移的编译时间
- 提升线程特定指针 get() 方法所花费的大致时间
- 放置此指针的位置以及为其分配内存的时间
- 字符数组指针返回错误(当前年份和时间)
- 通过引用传递与通过指针传递:Boost线程和截止时间计时器
- 是静态存储持续时间初始化为NULL或全零的对象中未初始化的指针
- c3867'sf::时间::作为秒':非标准语法;使用"&"创建指向成员的指针
- 时间指针存在吗?