1

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;
}
1
  • 2
    Maybe missing #include "card.h"? Commented Mar 28, 2014 at 13:13

2 Answers 2

3

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.

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

Comments

0

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

1 Comment

You shouldn't put function definitions in header files (e.g. stackoverflow.com/a/3805833/3235496).

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.