带有3个参数的Sprintf不起作用

J. *_*Doe -4 c++ printf

我正在进行游戏修改,但由于sprintf使用可能不正确,我的代码的一小部分似乎会导致游戏崩溃.

Menu& Menu::scroller(char** textArray, int* index, int numItems, bool fast) {
    char buffer[60];
    numItems--;

    if (hovered()) {
        lrInstruction = true;
        fastScrolling = fast;
        if (rightPress || (rightHold && fast)) {
            playSound("NAV_UP_DOWN");
            if (*index >= numItems) {
                *index = 0;
            }
            else {
                *index = *index + 1;
            }
        }
        else if (leftPress || (leftHold && fast)) {
            playSound("NAV_UP_DOWN");
            if (*index <= 0) {
                *index = numItems;
            }
            else {
                *index = *index - 1;
            }
        }
    }

    if (hovered()) {
        sprintf(buffer, "%s [%s/%s]", textArray[*index], *index, numItems);
        return data(buffer);
    }

    return data(textArray[*index]);
}
Run Code Online (Sandbox Code Playgroud)

这是我正在使用的原始代码,当我使用此sprintf时,由于可能不正确使用该函数而导致游戏崩溃.

当我这样做:

sprintf(buffer, "%s", textArray[*index]);
Run Code Online (Sandbox Code Playgroud)

它工作得很好.我已经尝试了多种类似的东西,$2%s/$3%s但似乎没有做到这一点.

有什么建议?

Ala*_*les 7

"%s"表示打印空终止字符串,如果要打印整数使用"%d".

更好的是,除非你有充分的理由不使用c ++ stringstreams,boost :: format或其他c ++类型的安全字符串格式库.