[英]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) : 我有一个接收pokemon struct的函数,我试图到达函数中的名称字段,它在标题中显示了错误信息,我尝试了之前建议的所有方法,但均不起作用,该函数是(不完整):
int pokemonMoveName(Pokemon pokemon){
char* name= pokemon->moves->name; //the error is in this line
return 0;
}
The element moves
is: 元素
moves
为:
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. 要么你必须删除
*
的typedef
或*
的结构。
If moves
really is a pointer to a pointer you'll have to access it the following way (or similar): 如果
moves
确实是指向指针的指针,则必须以以下方式(或类似方式)访问它:
char* name= (*(pokemon->moves))->name;
... which is equal to: ...等于:
char* name= pokemon->moves[0]->name;
... or, if moves
points to an array: ...,或者,如果
moves
点moves
到数组:
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. PokemonMove
类型已经像指针类型一样被声明。
typedef struct pokemon_move_t {
char* name;
PokemonType type;
int power_points, max_power_points;
int strength;
} *PokemonMove;
^^^^^^^^^^^^
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.