我正在為 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;
}
這是我的錯誤。我對 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);
| ^~~~~~~~~~~~~~~~~~~~~
uj5u.com熱心網友回復:
該函式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 );
player您還可以使用以下函式構建整個字串snprintf:
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 );
uj5u.com熱心網友回復:
strcat需要 2 個字串指標:
char *strcat(char *dest, const char *src);
在strcat(player, tesxx)并且strcat(player, testt)您傳遞 achar而不是 a char *,呼叫未定義的行為。為了將單個字符附加到具有額外空間的陣列中的 C 字串,您可以撰寫顯式代碼:
#include <string.h>
void append_char(char *dest, char c) {
size_t len = strlen(dest);
dest[len ] = c;
dest[len] = '\0';
}
或者您可以使用strncat定義<string.h>為:
char *strncat(char *dest, const char *src, size_t n);
此函式最多n從src. dest您可以傳遞 a 的地址char和長度1:
#include <string.h>
void append_char(char *dest, char c) {
strncat(dest, &c, 1);
}
然而,對于您的代碼,使用起來似乎要簡單得多snprintf:
char player[1024]; // probably too large
int tesx = 1;
int test = 3;
consoleInit(GFX_TOP, NULL);
// draw a `0` at screen coordinates tesx,test
snprintf(player, sizeof player, "\x1b[%d;%dH0", tesx, test);
uj5u.com熱心網友回復:
考慮 的簽名strcat。
char *strcat( char *dest, const char *src );
它不需要 achar *和 a char,而是需要兩個char *,并且需要兩個以 null 結尾的字串。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/473142.html
標籤:C
