这个功能

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] = '\0';

    return uniqid;
}


当被调用时,它返回奇怪的图标。

最佳答案

'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++ - 字符数组指针返回不正确(当前年份ctime),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32677951/

10-11 17:55