gpt4 book ai didi

C - 用指针改变字符

转载 作者:行者123 更新时间:2023-11-30 16:32:09 27 4
gpt4 key购买 nike

我正在尝试制作一款游戏,目前正在制作可重新绑定(bind)的控件。

这些是变量,带有分配的指针

//Controls
char MoveLeft = 'a';
char MoveRight = 'd';
char MoveUp = 'w';
char MoveDown = 's';
char BackButton = 't';
char ConfirmButton = 'y';
char ShowStats = 'u';
char ShowInventory = 'i';
char ShowLegend = 'o';
char ShowControls = 'p';

//Pointers
char *MoveLeftP = &MoveLeft;
char *MoveRightP = &MoveRight;
char *MoveUpP = &MoveUp;
char *MoveDownP = &MoveDown;
char *BackButtonP = &BackButton;
char *ConfirmButtonP = &ConfirmButton;
char *ShowStatsP = &ShowStats;
char *ShowInventoryP = &ShowInventory;
char *ShowLegendP = &ShowLegend;
char *ShowControlsP = &ShowControls;

这是我的控制方法菜单功能

void ControlsMenu(char *MoveLeft, char *MoveRight, char *MoveUp, char *MoveDown, char *BackButton, char *ConfirmButton, char *ShowStats, char *ShowInventory, char *ShowLegend, char *ShowControls) {
//Input
char Input = '0';

//Menu Variables
char ControlNames[] = {MoveLeft,MoveRight,MoveUp,MoveDown,BackButton,ConfirmButton,ShowStats,ShowInventory,ShowLegend,ShowControls};
char *MainMenuNames[] = { "- Move Left", "- Move Right","- Move Up","- Move Down","- Back","- Confirm","- Show Stats","- Show Inventory","- Show Legend"," - Show Controls"};
int MenuChosen = 0;
int *MenuPointer = 0;
MenuPointer = &MenuChosen;
int MinArray = 0;
int MaxArray = 8;
int *ControlPointer = 0;

//Main Loop
do {
//Loop
system("cls");
for (int i = 0; i < 9; i++) {
printf("%c", ToUpper(ControlNames[i]));
ShowMenu(MainMenuNames, i, MenuChosen);
}

Input = getch();

ControlMenu(Input, MenuChosen, MenuPointer, MinArray, MaxArray);

if ((Input == ConfirmButton || Input == ToUpper(ConfirmButton))) {
system("cls");
printf("Chaning key binding for %s\n", MainMenuNames[MenuChosen]);
printf("Old Binding: %c\n", ToUpper(ControlNames[MenuChosen]));
printf("Press the key you want to bind");
printf("%d", MenuChosen);
Input = getch();
ControlNames[MenuChosen] = Input;
//Commit Controls

switch (MenuChosen) {
case 0:
MoveLeft = Input;
break;
case 1:
MoveRight = Input;
break;
case 2:
MoveUp = Input;
break;
case 3:
MoveDown = Input;
break;
case 4:
BackButton = Input;
break;
case 5:
ConfirmButton = Input;
break;
case 6:
ShowStats = Input;
break;
case 7:
ShowInventory = Input;
break;
case 8:
ShowLegend = Input;
break;
case 9:
ShowControls = Input;
break;
}


}
} while (1);
}

这就是它的名字

    ControlsMenu(*MoveLeftP,*MoveRightP,*MoveUpP,*MoveDownP,*BackButtonP,*ConfirmButtonP,*ShowStatsP,*ShowInventoryP,*ShowLegendP,*ShowControlsP);

现在的问题是,由于某些奇怪的原因,字符的文本确实发生了变化,但实际的绑定(bind)保持不变。我可以成功更改的唯一绑定(bind)是ConfirmButton 绑定(bind),无论我将其更改为什么,它都可以工作,但任何其他内容(例如MoveUp 或MoveDown)都不会更改实际绑定(bind),而只会更改显示的文本。

最佳答案

不确定 ConfirmButton 发生了什么,但我发现 switch 语句中的内容存在问题。以 MoveLeft = Input; 为例。MoveLeft 是函数中的 char* (即内存地址),但 Input 是一个 char (即ASCII 值)。要将Input分配给MoveLeft指向的内存,您需要执行*MoveLeft = Input。您的编译器应该向您发出有关不兼容赋值的警告。其余的也类似。

顺便说一句,C 变量名称的约定是使用小写字母并加下划线,或者有些人使用驼峰式命名,首字母小写。而且给所有函数参数赋予与某些全局变量相同的名称会令人困惑。

关于C - 用指针改变字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50262128/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com