0

I tried making code to simulate boss fights. Firstly initialize the array of struct, making pointer out of it, and passing it into 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);
    }
}

I tried to code like this, i know i messed up somewhere when making the pointers. Anyways, where did i do wrong? Is it because i didnt use typedef? Been trying to search the similar problem in stackoverflow but i just cant understand it properly

1

1 Answer 1

1

In this assignment

ptr = &weapon_list;

the left operand has the type struct machine_gun * due to the declaration

struct machine_gun *ptr = NULL;

while the right operand has the type struct machine_gun ( * )[2] and there is no implicit conversion between the pointer types. The compiler should issue a message for this assignment statement.

Instead you need to write

ptr = weapon_list;

In this case the array designator is implicitly converted to pointer to its first element of the type struct machine_gun *.

Also as the function battle is declared like

void battle(struct machine_gun* wpn);

then you need to call it like

battle(ptr); 

Otherwise the expression &ptr has incompatible pointer type struct machine_gun **

Also these calls of printf

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

as similar calls as for example

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

are incorrect.

Expressions like for example this wpn[i] has the type struct machine_gun.

So you need to write

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

And change the similar way other calls of printf.

Else this declaration within the statement

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

hide the declaration before the if statement.

enum states state; 

So this switch statement

    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);
    }

uses the uninitialized variable state.

Also it seems you forgot to place the statement break in code snippets after each case label.

Sign up to request clarification or add additional context in comments.

1 Comment

i thought when using pointer, i should use "->" instead of "." it seems i wrong, ty for the corrections btw

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.