简体   繁体   中英

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

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;

I think you mean the following declaration of the data member

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

In this case this statement

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

will be valid.

The type PokemonMove is already declared like a pointer type.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM