Sheraz Faraj Sheraz Faraj - 12 days ago 6
C Question

request member for 'name' in something not structure or union

i have this two structures

typedef struct pokemon_move_t {
char* name;
PokemonType type;
int power_points, max_power_points;
int strength;
} *PokemonMove;

typedef struct pokemon_t {
char* name;
PokemonType type;
int experience;
int health_points;
PokemonMove* moves;
int number_of_moves, max_number_of_moves;
} *Pokemon;


and i have a function that receives pokemon struct and i'm trying to reach the name field in the function and it shows me the error message in the title,i tried everything that suggested before and it didn't work, the function is(not complete) :

int pokemonMoveName(Pokemon pokemon){
char* name= pokemon->moves->name; //the error is in this line
return 0;
}

Answer

The element moves is:

PokemonMove * moves;

Which is:

struct pokemon_move_t ** moves;

And not:

struct pokemon_move_t * moves;

... it is a pointer to a pointer to a structure and not to a structure itself.

I think that you don't want this!

Either you have to remove the * at the typedef or the * in the struct.

If moves really is a pointer to a pointer you'll have to access it the following way (or similar):

char* name= (*(pokemon->moves))->name;

... which is equal to:

char* name= pokemon->moves[0]->name;

... or, if moves points to an array:

char* name= pokemon->moves[index]->name;