这个函数
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”,通常应该返回“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] = '\0';
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';
关于c++ - Char 数组指针返回不正确(当前年份 ctime),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32677951/