我正在为3ds开发一款游戏,我希望它是cmd类型的游戏(我就是喜欢它).我试图让这个字符移动到我所有的x和y整数,但我得到了一个错误.这是我的密码.

/*
    Hello World example made by Aurelio Mannara for libctru
    This code was modified for the last time on: 12/12/2014 21:00 UTC+1
*/

#include <3ds.h>
#include <stdio.h>
#include <string.h>
int main(int argc, char **argv)
{
    gfxInitDefault();

    char player[1024] = "\x1b[";
    int tesx = 1;
    char tesxx = tesx + '0';
    char ot[] = ";";
    char oty[] = "H0";
    int test = 3;
    char testt = test + '0';
    //Initialize console on top screen. Using NULL as the second argument tells the console library to use the internal console structure as current one
    consoleInit(GFX_TOP, NULL);
    strcat(player, tesxx);
    strcat(player, ot);
    strcat(player, testt);
    strcat(player, oty);
    //Move the cursor to row 15 and column 19 and then prints "Hello World!"
    //To move the cursor you have to print "\x1b[r;cH", where r and c are respectively
    //the row and column where you want your cursor to move
    //The top screen has 30 rows and 50 columns
    //The bottom screen has 30 rows and 40 columns
    printf(player);

    // Main loop
    while (aptMainLoop())
    {
        //Scan all the inputs. This should be done once for each frame
        hidScanInput();

        //hidKeysDown returns information about which buttons have been just pressed (and they weren't in the previous frame)
        u32 kDown = hidKeysDown();

        if (kDown & KEY_START) break; // break in order to return to hbmenu

        // Flush and swap framebuffers
        gfxFlushBuffers();
        gfxSwapBuffers();

        //Wait for VBlank
        gspWaitForVBlank();
    }

    gfxExit();
    return 0;
}

这是我的错误.我对C非常陌生,所以如果这是一个简单的错误,我很抱歉.我试着搜索,但在网上什么也找不到.

C:/Users/Jeremy/Desktop/gaame/source/main.c:24:9: warning: 'strcat' offset 0 is out of the bounds [0, 0] [-Warray-bounds]
   24 |         strcat(player, testt);
      |         ^~~~~~~~~~~~~~~~~~~~~
C:/Users/Jeremy/Desktop/gaame/source/main.c:22:9: warning: '__builtin_stpcpy' offset 0 is out of the bounds [0, 0] [-Warray-bounds]
   22 |         strcat(player, tesxx);
      |         ^~~~~~~~~~~~~~~~~~~~~

推荐答案

函数strcat要求它的两个参数都是指向有效字符串的指针,根据定义,有效字符串是以空字符结尾的字符序列.

然而

strcat(player, tesxx);

第二个函数参数tesxx不是指向有效字符串的指针.而是一个简单的char.

因此,我建议您将这一行更改为以下内容:

player[2] = tesxx;
player[3] = '\0';

如果此行之前的字符串长度不能保证为2,则可以改为:

size_t len = strlen( player );
player[len+0] = tesxx;
player[len+1] = '\0';

或者,正如其他答案之一所建议的,您可以使用strncat,这允许您在字符串中附加单个字符:

strncat( player, &tesxx, 1 );

或者,你也可以改变台词

strcat(player, tesxx);
strcat(player, ot);
strcat(player, testt);
strcat(player, oty);

以下是:

snprintf( player + 2, (sizeof player) - 2, "%c%s%c%s", tesxx, ot, testt, oty );

更多信息,请参见功能snprintf.

如果这些行之前的字符串长度不能保证为2,则可以改为:

size_t len = strlen( player );
snprintf( player + len, (sizeof player) - len, "%c%s%c%s", tesxx, ot, testt, oty );

还可以使用函数snprintf构建整个字符串player:

    char player[1024];
    int tesx = 1;
    char tesxx = tesx + '0';
    char ot[] = ";";
    char oty[] = "H0";
    int test = 3;
    char testt = test + '0';
snprintf( player, sizeof player, "\x1b[%c%s%c%s", tesxx, ot, testt, oty );

C++相关问答推荐

警告:C++中数组下标的类型为‘char’[-Wchar-subpts]

如何在C宏中确定 struct 中元素的类型?

Win32API Wizzard97 PropSheet_SetWizButton不工作

在循环中复制与删除相同条件代码的性能

使用错误的命令执行程序

如何在下面的C代码中正确管理内存?

一旦运行长度超过2,编译器是否会优化";strnlen(mystring,32)>;2";以停止循环?

正数之和是负数

在C++中允许使用字符作为宏参数

为什么这个分配做得不好呢?

Fprintf正在写入多个 struct 成员,并且数据过剩

C:面筋蛋白';为什么不刷新窗口?

不兼容的整数到指针转换传递';char';到类型';常量字符*

如何使crc32的结果与cksum匹配?

共享内存未授予父进程权限

如何向 execl 创建的后台程序提供输入?

clion.我无法理解 Clion 中发生的 scanf 错误

定义 int a = 0, b = a++, c = a++;在 C 中定义了行为吗?

为什么我的条件不起作用?使用 fgets 输出

C 编译器编写中register关键字的用处