I have a struct where I put all the information about the players. That's my struct:
struct player{
int startingCapital;
int currentCapital;
int startingPosition;
int currentPosition;
int activePlayer;
int canPlay;
};
And that's my main:
#include <stdio.h>
#include <stdlib.h>
#include "header.h"
int main(int argc, char *argv[])
{ int s,i,numOfPlayers;
struct player *players;
printf("Give the number of players: \n");
scanf("%d",&numOfPlayers);
players = (struct player *)calloc(numOfPlayers,sizeof(struct player));
system("PAUSE");
return 0;
}
I'm asking the user to give the number of players and then I try to allocate the needed memory. But I'm getting this compiler error that I can't figure out:
invalid application of `sizeof' to incomplete type `player'
It means the file containing
main
doesn't have access to theplayer
structure definition (i.e. doesn't know what it looks like).Try including it in
header.h
or make a constructor-like function that allocates it if it's to be an opaque object.EDIT
If your goal is to hide the implementation of the structure, do this in a C file that has access to the struct:
However if the implementation shouldn't be hidden - i.e.
main
should legally sayp->canPlay = 1
it would be better to put the definition of the structure inheader.h
.