Matt.L
Matt.L

Reputation: 21

C - Double char array parsing - int to parse seems to modify itself without reason

i'm trying to check/display which button i'm pressing, i've been told to use specifically a double char array.

With the code bellow i'm getting this output : 1 7 8 9 10 11 MuteRec LockPlay and the lcd doesn't refresh itself to write nothing.

int displayButton(int buttonActivated)
{
    int parse = 0;
    char buttonNameArray[21][4] = {
        {"1   "},{"2   "},{"3   "},{"4   "},
        {"5   "},{"6   "},{"7   "},{"8   "},
        {"9   "},{"10  "},{"11  "},{"12  "},
        {"13  "},{"14  "},{"15  "},{"16  "},
        {"Mute"},{"Rec "},{"Lock"},{"Play"},
        {"    "}
    };

    for(parse = 0; parse < 3; parse++)
    {
        DRV_HD44780_putString(16 + parse, 1, &buttonNameArray[buttonActivated][parse]);
    }
    return(21);
}

void testKeyboardTask()
{
    int nbButton = 0;
    int buttonActivated = 21;

    while(1)
    {
        DRV_KEYBOARD_readKeyboard();
        int buttonId;
        for (buttonId=0;buttonId<20;buttonId++)
        {
            if (buttonStateArray[buttonId] == 1 && nbButton < 20)
            {
                buttonActivated = buttonId;
                nbButton++;
            }
        }
        if(nbButton > 1)
        {
            DRV_HD44780_putString(0, 3, "Error Multiple Press");
            osDelay(1000);
            DRV_HD44780_putString(0, 2, "                    ");
        }
        else
            buttonActivated = displayButton(buttonActivated);

        nbButton = 0;
    }
 }

It seems buttonActivated change over the time (adding 6 to itself). I can't grasp why i get this output (i'm pretty new to c) and with a switch it work just fine,

If anyone can see why

Upvotes: 0

Views: 57

Answers (1)

Matt.L
Matt.L

Reputation: 21

As everyone says i've simply forget about the NULL terminator as 4 chars weren't enough to hold it.

Thanks you everyone.

Upvotes: 2

Related Questions