Linked Questions

Popular Questions

How to send to a function a variable that is in another module?

Asked by At

How can I send a pointer that´s in another module to a new module?

Hello, I´m just starting a Programming Proyect in c of a game for class. This game contains a struct called game that also contains differents types data like Players ir Objects. Those structs are identified by a long Id, classified so that the objects have an Id between #0 and #100 for example.

To make it easier I´ve been creating a function "what_id" that recibing just an Id it returns a pointer to the struct that corresponds to it. I know how by sending the 'game' struct where are contained all the ids, and an Id, return the variable, but there´re modules that do not use in any case that big 'game' variable, for example player.c.

How can I send 'game' to this function without having it?

typedef struct _Game{
  Player* player[MAX_PLAYERS + 1];
  Object* object[MAX_OBJECTS + 1];
  Space* spaces[MAX_SPACES + 1];
 T_Command last_cmd;
} Game;
  typedef struct _Object{
  Id id;
  char name[MAX_SPACES];
}

void* what_id(Game* game, Id id){
   if(id == NO_ID) return ERROR;
   if(0 <id<=MAX_SPACES){
      return what_space(game->space, id);
   }else if(MAX_SPACES<id<=(MAX_OBJECTS+MAX_SPACES)){
      return what_object(game->object, id);
   }else if((MAX_OBJECTS+MAX_SPACES<id<(MAX_PLAYERS+MAX_OBJECTS+MAX_SPACES)){
      return what_player(game->player, id);
   }else {
      fprinf(stderr,"Id asigment max stacked.";
      return ERROR;
    }
}
Space* what_space(const Space* space, Id id){
   int i;
   for(i=0;i<MAX_SPACES;i++){
       if(space[i]->id == id)return space[i];
   }
   fprintf(stderr, "Error no space_id %d founded", id);
   return ERROR;
}

Related Questions