如果声明无效

发布于 2024-08-27 18:27:35 字数 667 浏览 1 评论 0原文

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);
}

如果我是正确的,那么代码块应该在 NCURSES 屏幕上打印出来

 OO  
OOOO
OOOO
 OO

,但是,它会打印出默认文本(第一个字符语句)。谁能告诉我这是为什么? If 块内的 printw 语句打印出正确的文本,因此它被正确分配。先感谢您。

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'm correct that block of code should print out on a NCURSES screen

 OO  
OOOO
OOOO
 OO

Instead however, it prints out the default text (the first char statements). Can anyone tell me why this is? The printw statement inside the If-block prints out the proper text, so it's being assigned correctly. Thank you in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

始终不够 2024-09-03 18:27:35

if 语句内的声明会遮蔽其外部的声明;一旦 if 语句退出,那些隐藏的声明就会超出范围并永远消失。

要解决此问题,您可以执行类似的操作

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

,或者您可以使用诸如 strcpy 之类的函数来完成相同的操作。

顺便说一下,在这种情况下,在打开完整警告的情况下进行编译会向您显示错误,而无需在此处发布。

Your declarations inside the if statement are shadowing the declarations outside it; once the if-statement exits, those shadowed declarations are out of scope and gone forever.

To work around this, you could do something like

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

Or you could use a function like strcpy to accomplish the same thing.

This is a situation where compiling with full warnings turned on would have shown you the error without needing to post here, by the way.

夕嗳→ 2024-09-03 18:27:35

您在“if”块中将它们声明为局部变量。它们不会影响函数范围内的字符串。

You're declaring them as local variables in the 'if' block. They don't affect the strings in function scope.

中二柚 2024-09-03 18:27:35

您正在 if (stype == 1) 的本地块中创建另一组具有相同名称的局部变量(sprite1、sprite2 等),这会遮蔽外部级别的声明。使用这个代替:

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

You are making another set of local variables with the same name (sprite1, sprite2, etc) in the block local to the if (stype == 1) which shadows the declarations at the outer level. Use this instead:

if (stype == 1)
{
    sprintf(sprite1, "%s", " OO ");
    // etc
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文