Aplicación inválida de sizeof a tipo incompleto con una estructura


Tengo una estructura donde pongo toda la información sobre los jugadores. Esa es mi estructura:

struct player{
   int startingCapital;
   int currentCapital;
   int startingPosition;
   int currentPosition;
   int activePlayer; 
   int canPlay;      
};

Y esa es mi principal:

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

Le estoy pidiendo al usuario que dé el número de jugadores y luego trato de asignar la memoria necesaria. Pero estoy recibiendo este error del compilador que no puedo entender:

invalid application of `sizeof' to incomplete type `player'  
Author: Braiam, 2012-01-18

4 answers

Significa que el archivo que contiene main no tiene acceso a la definición de la estructura player (es decir, no sabe cómo se ve).

Intente incluirlo en header.h o cree una función similar a un constructor que lo asigne si va a ser un objeto opaco.

EDITAR

Si su objetivo es ocultar la implementación de la estructura, haga esto en un archivo C que tenga acceso a la estructura:

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

    /* ... */
    return p;
}

Sin embargo, si la implementación no debe ocultarse, es decir, main debe legalmente decir p->canPlay = 1 sería mejor poner la definición de la estructura en header.h.

 25
Author: cnicutar,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-01-18 18:26:54

La causa de errores como "Aplicación inválida de sizeof a tipo incompleto con una estructura ... "es siempre la falta de una declaración de inclusión. Trate de encontrar la biblioteca correcta para incluir.

 5
Author: oiyio,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-04-26 08:53:26

Creo que el problema es que pones #ifdef en lugar de #ifndef en la parte superior de tu archivo header.h.

 0
Author: morte69,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-07-05 04:23:14

Su error también se muestra cuando intenta acceder a sizeof() de un array externo no inicializado:

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

Tenga en cuenta que obtendría un error array size missing sin la palabra clave extern.

 0
Author: alexfrigo,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-03-30 09:11:04