If the statement is not valid

void spriteput(int x,int y, int stype)
{
    char sprite1[5]="OOOO";
    char sprite2[5]="OOOO";
    char sprite3[5]="OOOO";
    char sprite4[5]="OOOO";
    if (stype == 1)
    {
        char sprite1[5] = " OO ";
        char sprite2[5] = "OOOO";
        char sprite3[5] = "OOOO";
        char sprite4[5] = " OO ";
        mvprintw(2,y,"%s \n",sprite1);
    }
    mvprintw(x+1,y,"%s \n",sprite2);
    mvprintw(x+2,y,"%s \n",sprite3);
    mvprintw(x+3,y,"%s \n",sprite4);
}

If I am right, the code block should be printed on the NCURSES screen.

 OO  
OOOO
OOOO
 OO

Instead, it prints the default text (first char). Can someone tell me why this is? The statement printwinside the If block produces the correct text, so it is assigned correctly. Thank you in advance.

+3
source share
3 answers

Your ads inside the operator ifobscure ads outside of it; after exiting the if-statement, those shaded ads go beyond and go away forever.

To get around this, you could do something like

if (stype == 1)
{
    sprite1[0] = ' ';
    sprite1[3] = ' ';
    // ...

​​, strcpy, .

, , .

+8

"if". .

+2

You make another set of local variables with the same name (sprite1, sprite2, etc.) in a block local to if (stype == 1)that obscures the declarations externally. Use this instead:

if (stype == 1)
{
    sprintf(sprite1, "%s", " OO ");
    // etc
}
+1
source

Source: https://habr.com/ru/post/1739917/


All Articles