c - c 中的指针- "is a pointer; did you mean to use ' ->'?"

标签 c pointers

我尝试使用指针为嵌套结构中的字段分配新值, 但我不断收到标题中的编译错误, 这是我的代码:

    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/

相关文章:

c - 通过UDP协议(protocol)发送巨大的(大约40K字节)数据,怎么可能呢?

python - 使用 GDB-Python Types API 查找嵌套结构的绝对偏移量

c - 使用指针查找字符串的长度

C 使用字符指针

c++ - sizeof() 的参数

c - 当我用 C 语言发送带有 char 数组的命令时,system() 调用或 exec() 函数不起作用

c++ - sscanf 从八进制转换 : How does it know?

C中的凯撒密码加密程序

pointers - bytes.Reader,替换底层 []byte 数组

C 列表段错误