调用高分函数时名称指针出现问题。所有名称都将替换为最新名称。无法使用malloc

问题描述 投票:0回答:1

当我运行 high_score 两次时,display_high_score 函数正确显示分数,但显示我第二次输入的名称。基本上, render_string(displayBuffer, name, 28 + 14 * i, 24); 似乎两者的名字相同。

所以我有一个高分函数,允许玩家修改随后与玩家得分一起显示的字符串。玩家得分按降序正确显示。但是,所有名称都会更改为我使用 high_score 输入的最新名称。有什么想法吗? ChatGPT 这么说

“您面临的问题与如何将名称存储在 highscore 函数内的player_high_scr 结构中有关。您将 name_letter_array 直接分配给player_high_scr.name,这意味着player_high_scr 的所有实例将共享相同的名称指针。因此,当您在循环的后续迭代中修改 name_letter_array 时,player_high_scr 的所有实例都将指向相同的 name_letter_array,从而导致观察到的行为。”

不知道如何解决。它建议使用malloc。问题是我无法在我正在使用的环境中使用 malloc。

struct high_score {
    char* name;
    int score;
};

void highscore( int* score, uint8_t* displayBuffer, struct high_score* high_score_arr){
    uint8_t original_switch = getsw();
    char *alphabet_str = " abcdefghijklmnopqrstuvwxyz";
    int highScrExit = 0;
    int first_box_num = 0, second_box_num = 0, third_box_num = 0, fourth_box_num = 0, fifth_box_num = 0, sixth_box_num = 0;
    int box_num_arr[6] = {first_box_num, second_box_num, third_box_num, fourth_box_num, fifth_box_num, sixth_box_num};
    char name_letter_array[8] = {' ', ' ', ' ', ' ', ' ', ' ', '\0'};
    int box_number = 0;
    while(!highScrExit){
        clearBuffer(displayBuffer);
        uint8_t buttons = getBTNs();
        uint8_t switches = getsw();
        if(buttons & 0x1){
            box_number = (box_number+1)%6;
            delayms(20);
        }
        if(buttons & 0x2){
            if(box_number > 0 ){
            box_number--;}
            else {
                box_number = 5;
            }
            delayms(20);
        }

        if(buttons & 0x8){
            box_num_arr[box_number]++; //Increment the letter for box we're on by one, meaning that [a] will go to [b]
            name_letter_array[box_number] = alphabet_str[box_num_arr[box_number]%27]; //change the letter in the name to that letter
            delayms(20);
        }
        if(buttons & 0x4){
            if(box_num_arr[box_number]>0){
            box_num_arr[box_number]--;}
            else if (box_num_arr[box_number] <= 0){
                box_num_arr[box_number] = 26;
            }
            name_letter_array[box_number] = alphabet_str[box_num_arr[box_number]%27];
            delayms(20);
        }


        if((!(original_switch & 0x8) && switches & 0x8) || ((original_switch &0x8) && !(switches & 0x8))){
            struct high_score player_high_scr = {name_letter_array, *score};
            int i;
            for(i=4; i>=0; i--){
                if(player_high_scr.score > (&high_score_arr[i])->score){
                    high_score_arr[i+1] = high_score_arr[i];
                    high_score_arr[i] = player_high_scr;
                }
            }
            highScrExit = 1;
            display_high_score_screen(displayBuffer, high_score_arr);
        }
        render_triangle(displayBuffer, 52, 28 - 4*box_number);
        int j;
        render_switch(displayBuffer, 80, 28);
        render_string(displayBuffer, "save  ", 80, 20);
        //For loop not working for some reason? It's ugly but works
        render_button(displayBuffer, 80 + 9, 28);
        render_string(displayBuffer, "up    ", 80 + 9, 20);
        render_button(displayBuffer, 80 + 9+9, 28);
        render_string(displayBuffer, "down  ", 80 + 9+9, 20);
        render_button(displayBuffer, 80 + 9+9+9, 28);
        render_string(displayBuffer, "left  ", 80 + 9+9+9, 20);
        render_button(displayBuffer, 80 + 9+9+9+9, 28);
        render_string(displayBuffer, "right  ", 80 + 9+9+9+9, 20);

        render_string(displayBuffer, "score ", 5, 25);
        render_string(displayBuffer, "enter ", 28, 26);
        render_string(displayBuffer, "name ", 35, 24);
        render_string(displayBuffer, name_letter_array, 45, 28);
        render_score(displayBuffer, score, 15, 20);
        displayUpdate(displayBuffer);
    }
}

void display_high_score_screen(uint8_t* displayBuffer, struct high_score* high_score_arr){
    int mainMenuExit = 0;
    while(!mainMenuExit){
        clearBuffer(displayBuffer);
        uint8_t buttons = getBTNs();
        if(buttons & 0x8){
            mainMenuExit = 1;
            delayms(30);
        }
        int i;
        render_string(displayBuffer, "top  ", 5, 22);
        render_string(displayBuffer, "scores ", 12, 25);
        render_button(displayBuffer, 100, 28);
        render_string(displayBuffer, "quit ", 100, 20);
        for(i = 0; i<5; i++){
            int number = i+1;
            char* name = (&high_score_arr[i])->name;
            int score = (&high_score_arr[i])->score;
            render_score(displayBuffer, &number, 28 + 14*i, 30);
            if(*name != '\0' && score != -1){
            render_string(displayBuffer, name, 28 + 14 * i, 24);
            render_score(displayBuffer, &score, 35 + 14 * i, 18);}
        }
        displayUpdate(displayBuffer);
    }
}

//Call the high_score function like this:
if (gameOver) {
            int j;
            for(j = 0; j<5; j++){
                if(score > (&high_score_arr[j])->score){
                    j = 5;
                    highscore(&score, displayBuffer, high_score_arr);
                }
            }

//This is the part of the code that assigns name:

struct high_score player_high_scr = {name_letter_array, *score};
            int i;
            for(i=4; i>=0; i--){
                if(player_high_scr.score > (&high_score_arr[i])->score){
                    high_score_arr[i+1] = high_score_arr[i];
                    high_score_arr[i] = player_high_scr;
                }
            }
            highScrExit = 1;
            display_high_score_screen(displayBuffer, high_score_arr);

//I should point out that this is how i initialize the array:

void init_high_scr_arr(){
    int i;
    char* null;
    *null = '\0';
    for(i = 0; i<5; i++){
        high_score_arr[i].name = null;
        high_score_arr[i].score = -1;
    }
}

我尝试了很多方法,让 highscore 返回一个 high_score 结构,将其放入 display_high_score 并调用它。我尝试将 high_score.name 放入数组而不是指针,我尝试将结果保存在某些全局变量中,等等。结果没有改变。所有名字都与我在 high_score 中输入的姓氏相同。

c pointers game-development pic32
1个回答
0
投票

如果您无法动态创建名称空间,您可以在 high_score 结构中创建名称空间,将其更改为类似

char name[8]
的内容以匹配
name_letter_array
的大小。然后,当您存储高分时,您必须将字符串从
name_letter_array
复制到高分结构体
name
中,而不仅仅是将指针传递给
name_letter_array

© www.soinside.com 2019 - 2024. All rights reserved.