简体   繁体   English

C ++在函数中使用char指针数组作为唯一的char指针

[英]c++ using char pointer array in function as only char pointer

I'm going to go over everything being used before I ask questions about it... 我会先询问所有正在使用的东西,然后再问有关问题...

I've created a array of char pointers here, I use the array in a function right after 我在这里创建了一个char指针数组,之后我在函数中使用了该数组

char *TShirtsText[] = { "Black", "Yellow", "Blue" };
ModelChanger->AddVariantItem("R* T-Shirts", TShirtsText[0], -1, 0, 2, 1, DevShirt, (bool*)true);

Now I have the function here, take notice of optionstext 现在我有了这里的功能,注意optionstext

// Add a variant item to the menu
void GTAVMenu::AddVariantItem(char *displayText, char *optionstext, float var, float min, float max, float changeby, GTAVMenuCallback functionCallback, void *functionParameters) {

GTAVMenuItem menuItem;

// Set menu type
menuItem.menuItemType = MENU_TYPE_VARIANT;

// Add variant text to item text
char newDisplayText[32];
if (functionParameters == NULL)
    sprintf_s(newDisplayText, sizeof(newDisplayText), "%s: < %g >", displayText, var);
else
    sprintf_s(newDisplayText, sizeof(newDisplayText), "%s: < Not Set >", displayText);

// Copy menu item text
strcpy_s(menuItem.itemText, 32, newDisplayText);

// Function callback
menuItem.functionCallback = functionCallback;

// No display callback
menuItem.displayCallback = NULL;

// Sub menu
menuItem.subMenu = NULL;

// Function params
menuItem.functionParameters = functionParameters;

// Menu item toggling
menuItem.itemToggleable = false;
menuItem.itemToggled = false;

// Keep memory of displayText, optionstext, var, min, max, changeby
menuItem.vartext = displayText;
if (functionParameters != NULL) menuItem.optionstext = optionstext;
menuItem.var = var;
menuItem.min = min;
menuItem.max = max;
menuItem.changeby = changeby;

// Add our menu item
menuItems->push_back(menuItem);

} }

here's a sample of code where I press a button and this is what happens roughly, take notice of optionstext 这是我按下按钮的代码示例,这大致发生了,请注意optionstext

switch(menuItem->menuItemType) 
{
    case MENU_TYPE_VARIANT:
    {
        if (menuItem->var <= menuItem->min)
            menuItem->var = menuItem->max;
        else
            //menuItem->var--;
            menuItem->var -= menuItem->changeby;

        selectedNum = menuItem->var;
        play_sound_frontend(0, "NAV_UP_DOWN", "HUD_FRONTEND_DEFAULT_SOUNDSET");

        // Add variant text to item text
        char newDisplayText[32];
        if (menuItem->functionParameters == NULL)
            sprintf_s(newDisplayText, sizeof(newDisplayText), "%s: < %g >", menuItem->vartext, menuItem->var);
        else
            sprintf_s(newDisplayText, sizeof(newDisplayText), "%s: < %s >", menuItem->vartext, menuItem->optionstext);

        // Copy menu item text
        strcpy_s(menuItem->itemText, 32, newDisplayText);

        // Calling function - never used to be here
        menuItem->functionCallback(selectedMenuItem, menuIndexStack, menuItem->itemText, menuItem->functionParameters);

        break;
    }
}

And this is where the question comes along. 这就是问题所在。 So, I'm using an array of char pointers, and I'm using the first element from that array as you can see from the first bit of code. 因此,我正在使用一个char指针数组,并且正在使用该数组中的第一个元素,正如您从代码的第一位可以看到的那样。 In the last bit of code, one of the sprintf_s places menuItem->optionstext into newDisplayText . 在最后的代码中, sprintf_smenuItem->optionstext放入newDisplayText In the output, when I press the left button, sprintf_s uses the last element of the char pointer array, and if I press the right button on my controller, it changes it to the next element. 在输出中,当我按向左按钮时, sprintf_s使用char指针数组的最后一个元素,如果我按控制器上的向右按钮,它将更改为下一个元素。

Why does it change it to the next element when I haven't stated which element I want it to copy? 当我没有声明要复制哪个元素时,为什么将其更改为下一个元素? and why is the program allowing me to do this especially when all I'm using in the function is one element from the array? 为什么程序允许我执行此操作,特别是当我在函数中使用的只是数组中的一个元素时?

You reserve only 32 bytes for the string: 您只为字符串保留32个字节:

char newDisplayText[32];

But then you put the full 32 chars in it, so there is no space for the closing '\\0' . 但是随后您将完整的32个字符放入其中,因此没有空格可用于结束'\\0' Any access will therefore overrun and read or write over whatever happens to be next in memory. 因此,任何访问都将溢出并读取或写入内存中接下来发生的任何事情。 That typically produces all kind of funny and wild errors. 这通常会产生各种有趣的错误。

You need to declare char newDisplayText[33]; 您需要声明char newDisplayText[33]; to have space for 32 chars. 可以容纳32个字符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM