我正在为 3ds 开发一款游戏,我希望它是一款 cmd 类型的游戏(我只是觉得喜欢)。我试图让这个 char 移动到我拥有的任何 x 和 y int 数字,但出现错误。这是我的代码。
/*
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;
}
Run Code Online (Sandbox Code Playgroud)
这是我的错误。我对 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);
| ^~~~~~~~~~~~~~~~~~~~~
Run Code Online (Sandbox Code Playgroud)
该函数strcat要求其两个参数都是指向有效字符串的指针,根据定义,该字符串是一个以空字符结尾的字符序列。
然而,在行
strcat(player, tesxx);
Run Code Online (Sandbox Code Playgroud)
第二个函数参数tesxx不是指向有效字符串的指针。相反,它是一个简单的char.
因此,我建议您将此行更改为以下内容:
player[2] = tesxx;
player[3] = '\0';
Run Code Online (Sandbox Code Playgroud)
如果该行之前的字符串长度不能保证为2,那么您可以这样写:
size_t len = strlen( player );
player[len+0] = tesxx;
player[len+1] = '\0';
Run Code Online (Sandbox Code Playgroud)
或者,正如其他答案之一所建议的,您可以使用strncat它,它允许您将单个字符附加到字符串中:
strncat( player, &tesxx, 1 );
Run Code Online (Sandbox Code Playgroud)
或者,您可以更改线路
strcat(player, tesxx);
strcat(player, ot);
strcat(player, testt);
strcat(player, oty);
Run Code Online (Sandbox Code Playgroud)
至以下内容:
snprintf( player + 2, (sizeof player) - 2, "%c%s%c%s", tesxx, ot, testt, oty );
Run Code Online (Sandbox Code Playgroud)
请参阅该函数snprintf以获取更多信息。
如果这些行之前的字符串长度不能保证为2,那么您可以这样写:
size_t len = strlen( player );
snprintf( player + len, (sizeof player) - len, "%c%s%c%s", tesxx, ot, testt, oty );
Run Code Online (Sandbox Code Playgroud)
player您还可以使用以下函数构建整个字符串snprintf:
char player[1024];
int tesx = 1;
char tesxx = tesx + '0';
char ot[] = ";";
char oty[] = "H0";
int test = 3;
char testt = test + '0';
Run Code Online (Sandbox Code Playgroud)
snprintf( player, sizeof player, "\x1b[%c%s%c%s", tesxx, ot, testt, oty );
Run Code Online (Sandbox Code Playgroud)