在 C 中通过双指针访问结构元素 [英] Accessing structure elements via double pointers in C

查看:38
本文介绍了在 C 中通过双指针访问结构元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用结构实现链表.我有一个结构-

I'm implementing linked lists using structures. I have a structure -

typedef struct llist node;
typedef node *nodeptr;
struct llist
{
    int data;
    nodeptr next;
};

现在可以说我声明了一个变量 nodeptr * ptr; .如何使用 ptr 访问成员 data next ?

Now lets say I declare a variable nodeptr *ptr;. How do I access the members data and next using ptr?

推荐答案

您先引用第一个指针,然后再引用第二个指针.

You deference the first pointer and then the second one.

要访问数据,请在结构语句中访问

To access the data and next in the structure statement would like this

(*ptr)->data = 5;
(*ptr)->next = temp;

由于-> 的优先级高于 * ,因此需要在ptr周围加上

括号.

brackets around ptr is required since -> has higher priority than *.

-> 等同于写入 *. (例如 ptr->数据 * ptr.data 相同).

这篇关于在 C 中通过双指针访问结构元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆