我尝试使用指针为嵌套结构中的字段分配新值, 但我不断收到标题中的编译错误, 这是我的代码:
struct Territory{
int x1;
int x2;
int y1;
int y2;
};
struct Country{
char *name;
pCity *cities;
int numCities;
pTerritory *countryTerr;
};
typedef struct Territory* pTerritory;
typedef struct Country* pCountry;`struct Territory
ter_for_country;
struct Country new_country;
pCountry country_pointer;
country_pointer = &new_country;
ter_for_country.x1 = 3;
ter_for_country.x2 = 3;
ter_for_country.y1 = 3;
ter_for_country.y2 = 3;
new_country.numCities=2;
new_country.countryTerr = &ter_for_country;
现在假设我想使用指针“country_pointer”更改new_country的“x1”,我该怎么做? 我尝试过:
country_pointer->countryTerr->x1 = 25;
但是我收到错误,你能帮忙吗? 谢谢
最佳答案
您的一级指针太多:pTerritory
被定义为指向 struct Territory
的指针,并且在 struct Country
中,您将countryTerr
指向pTerritory
的指针。这给你一个指向指针的指针,你想要一个指向结构的指针。解决方案:将 countryTerr
更改为 pTerritory
类型(而不是 pTerritory *
)。
关于c - c 中的指针- "is a pointer; did you mean to use ' ->'?",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53411944/