简体   繁体   中英

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 *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

// 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

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. In the last bit of code, one of the sprintf_s places menuItem->optionstext into 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.

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:

char newDisplayText[32];

But then you put the full 32 chars in it, so there is no space for the closing '\\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]; to have space for 32 chars.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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