Invalid application of sizeof to incomplete type with a struct

135,790

Solution 1

It means the file containing main doesn't have access to the player 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:

struct player *
init_player(...)
{
    struct player *p = calloc(1, sizeof *p);

    /* ... */
    return p;
}

However if the implementation shouldn't be hidden - i.e. main should legally say p->canPlay = 1 it would be better to put the definition of the structure in header.h.

Solution 2

The cause of errors such as "Invalid application of sizeof to incomplete type with a struct ... " is always lack of an include statement. Try to find the right library to include.

Solution 3

Your error is also shown when trying to access the sizeof() of an non-initialized extern array:

extern int a[];
sizeof(a);
>> error: invalid application of 'sizeof' to incomplete type 'int[]'

Note that you would get an array size missing error without the extern keyword.

Share:
135,790

Related videos on Youtube

captain
Author by

captain

Updated on February 09, 2022

Comments

  • captain
    captain about 2 years

    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'  
    
    • ChrisWue
      ChrisWue over 12 years
    • MetallicPriest
      MetallicPriest over 12 years
      Have you declared struct player in header.h?
    • Renan Greinert
      Renan Greinert over 12 years
      If header.h contains the definition of "player", this program should compile fine, both in C and C++
  • captain
    captain over 12 years
    In the header I just put "struct player;" I didn't like it in the beginning but when I do something like players.startinCapital=1500 I don't get an error and it prints the result without a problem. So basically I have to make a function which constructs the struct?
  • ouah
    ouah over 12 years
    @captain put the complete declaration of the struct player type in your header.h file
  • captain
    captain over 12 years
    @ouah I did this an it works. What should I put inside the init_player?
  • Tony Han
    Tony Han over 7 years
    I have the similar problem. And I use a 3rd-part lib, so I can't get the implementation. But I have to get the size of the struct(for passing that to other lib). How to do this?
  • lc2047
    lc2047 over 4 years
    @nhahtdh Thanks for your advice. But it happens to another problem. Also, initially #ifndef is used to avoid multiple declare. Right? if not, please let me know the detail.