簡體   English   中英

如何將指向結構數組的指針傳遞給 function

[英]How to pass pointer to array of struct into function

我試着編寫代碼來模擬老板打架。 首先初始化struct數組,從中生成指針,傳入function。

#include <stdio.h>


struct machine_gun{
    char name[10];
    int rounds;
    int damage;
};

enum states{
    Tidak,
    Ya
};


void battle(struct machine_gun* wpn);

int main(){
    
    struct machine_gun weapon_list[2] = {
        {"Holger", 400, 20},
        {"M4LMG", 320, 25}
    };

    struct machine_gun *ptr = NULL;
    ptr = &weapon_list;
    int choice;
    
    printf("Boss has appeared! Enter 1 to fight the boss, 0 to run\n"); scanf("%d", &choice);
    if(choice == 1){
        battle(&ptr); 
    }
    else if(choice != 1){
        printf("You ran away.");
    }
}

void battle (struct machine_gun* wpn){
    int choice;
    int weapon_choice;
    int boss_hp = 1000;
    enum states state; 

    printf("###Weapon Lists###\n");
    for(int i = 0; i < 2; i++){
        printf("Name: %s\n", wpn[i]->name);
        printf(" Rounds: %d\n", wpn[i]->rounds);
        printf(" Damage: %d\n", wpn[i]->damage);
    }
    printf("\nBoss HP is 1000, are you sure you want to fight?\n1. Yes\n2. No\n"); scanf("%d", &choice);
    
    
    if(choice == 1){
        enum states state = Ya;
    }
    else{
        enum states state = Tidak;
    }
        switch(state){
            case Tidak:
            printf("Game Over!\nYou've been killed by the Boss.");
            case Ya:
            printf("Choose your weapon!\n");
            printf("1. %s with %d ammo and does %d damages\n", wpn[0]->name, wpn[0]->rounds, wpn[0]->damage);
            printf("2. %s with %d ammo and does %d damages\n", wpn[1]->name, wpn[1]->rounds, wpn[1]->damage);
            scanf("%d", &weapon_choice);
        }
    if(weapon_choice == 1){
        printf("You shot the boss with %d bullets\nCongratulation! You killed the boss, thanks for playing!\n", (1000/wpn[0]->damage));
        wpn[0]->rounds = wpn[0]->rounds - (1000/wpn[0]->damage); 
        printf("Your ammo remains: %d", wpn[0]->rounds);
    }
    else if(weapon_choice == 2){
        printf("You shot the boss with %d bullets\nCongratulation! You killed the boss, thanks for playing!\n", (1000/wpn[1]->damage));
        wpn[1]->rounds = wpn[1]->rounds - (1000/wpn[1]->damage); 
        printf("Your ammo remains: %d", wpn[1]->rounds);
    }
}

我試着像這樣編碼,我知道我在制作指針時搞砸了某個地方。 不管怎樣,我哪里做錯了? 是因為我沒有使用 typedef 嗎? 一直試圖在 stackoverflow 中搜索類似的問題,但我無法正確理解它

在這個任務中

ptr = &weapon_list;

由於聲明,左操作數的類型為struct machine_gun *

struct machine_gun *ptr = NULL;

而右操作數的類型為struct machine_gun ( * )[2]並且指針類型之間沒有隱式轉換。 編譯器應該為此賦值語句發出一條消息。

相反,你需要寫

ptr = weapon_list;

在這種情況下,數組指示符被隱式轉換為指向其第一個類型為struct machine_gun *的元素的指針。

也作為 function 戰斗被宣布像

void battle(struct machine_gun* wpn);

那么你需要這樣稱呼它

battle(ptr); 

否則表達式&ptr具有不兼容的指針類型struct machine_gun **

還有這些電話 printf

    printf("Name: %s\n", wpn[i]->name);
    printf(" Rounds: %d\n", wpn[i]->rounds);
    printf(" Damage: %d\n", wpn[i]->damage);

與例如類似的電話一樣

printf("Your ammo remains: %d", wpn[0]->rounds);

不正確。

例如這個wpn[i]的表達式具有類型struct machine_gun

所以你需要寫

    printf("Name: %s\n", wpn[i].name);
    printf(" Rounds: %d\n", wpn[i].rounds);
    printf(" Damage: %d\n", wpn[i].damage);

並以類似的方式更改printf的其他電話。

否則聲明中的聲明

if(choice == 1){
    enum states state = Ya;
}
else{
    enum states state = Tidak;
}

隱藏 if 語句之前的聲明。

enum states state; 

所以這個 switch 語句

    switch(state){
        case Tidak:
        printf("Game Over!\nYou've been killed by the Boss.");
        case Ya:
        printf("Choose your weapon!\n");
        printf("1. %s with %d ammo and does %d damages\n", wpn[0]->name, wpn[0]->rounds, wpn[0]->damage);
        printf("2. %s with %d ammo and does %d damages\n", wpn[1]->name, wpn[1]->rounds, wpn[1]->damage);
        scanf("%d", &weapon_choice);
    }

使用未初始化的變量state

此外,您似乎忘記在每個案例 label 之后的代碼片段中放置語句break

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM