Phantom
Phantom

Reputation: 23

Code error structs

when i run the program i get

card.c:3:23: error: dereferencing pointer to incomplete type printf("%i", attacker->power);

main.c:

#include <stdio.h>
#include "card.h"
int main(){
    return 0; 
}

card.h:

#ifndef CARD_H_FILE
#define CARD_H_FILE
struct card_t {
    char name[10];
    int power, health, mana_cost;
};
int attack(struct card_t *, struct card_t *);
#endif

card.c:

int attack(struct card_t *attacker, struct card_t *defender){
    printf("%i", attacker->power);
    return 1;
}

Upvotes: 1

Views: 75

Answers (2)

apm
apm

Reputation: 535

include the content of card.c in the file card.h.

card.h

#ifndef CARD_H_FILE
#define CARD_H_FILE

struct card_t {
char name[10];
int power, health, mana_cost;
};

int attack(struct card_t *attacker, struct card_t *defender){
printf("%i", attacker->power);
return 1;
}
#endif

Upvotes: 0

DevSolar
DevSolar

Reputation: 70391

Unless you made omissions when posting your code, card.c doesn't include card.h, which means it knows nothing about struct card_t or its members (->power). It also doesn't inlucde stdio.h, which means it does not know about printf() either.

Remember that C compilers translate source (.c) files in isolation, they do not concatenate them. This means the includes in main.c do nothing at all for card.c.

Upvotes: 3

Related Questions