c - 我无法理解他的 typedef 指针用法,有人可以解释一下吗?
问题描述
前段时间我有一个学校项目,当时我正在学习 C,但现在已经有一段时间无法如此轻松地理解指针了。代码如下:
typedef struct {
Identification id;
Ring edge;
Ring *holes;
in nHoles;
} Parcel;
typedef Parcel *Cartography;
//this is the definition of Cartography
*cartography = malloc(sizeof(Parcel));
*(*cartography + idx) = readParcel(f);
//this is the code when I want to insert a new Parcel into Cartography
我不明白为什么在尝试添加新包裹时必须使用“ *” 2次,有人可以向我解释一下吗?
是因为制图学中的值是 malloc 的地址,所以第一个 * 给了我那个地址,第二个 * 把我带到那个地址?
非常感谢大家的帮助!!
解决方案
你不用 . 声明变量typedef
。此外,您不应将返回的指针分配给malloc()
非指针类型。Cartography
是一个指针类型,并且取消引用它*
意味着您将指针分配给Parcel
. 这是正确的代码:
typedef struct {
Identification id;
Ring edge;
Ring *holes;
in nHoles;
} Parcel;
Parcel *Cartography;
//this is the definition of Cartography
Cartography = malloc(sizeof(Parcel));
*(Cartography + idx) = readParcel(f);
推荐阅读
- timestamp - Log4j2:如何获得实际上具有纳秒精度的时间戳?
- angular - 在 Angular 12 中实现刷新令牌
- ios - React Native - 捆绑 iOS 应用程序时,Metro Bundler 的配置文件中未定义 .ENV
- html - 从远程 url 或绝对文件路径检索 HTML 字幕轨道
- datatable - 如何在 cakephp 4 中使用表对象?
- hp-alm - 如何从 ALM 中检索用户?
- sql - 使用 CASE 语句将值插入 SQLite 中的多个列
- python - 从 HTML iframe 中检索数据
- c# - Xamarin App SQL Server 连接在发布模式下不起作用,编码 1252
- python - 外键字段'startswith'的Django约束