从结构中访问变量(Accessing variables from a struct)

编程入门 行业动态 更新时间:2024-10-27 12:35:19
从结构中访问变量(Accessing variables from a struct)

我们如何访问一个结构的变量? 我有一个结构:

typedef struct { unsigned short a; unsigned shout b; } Display;

在我的其他课上我有一个方法:

int NewMethod(Display **display) { Display *disp=new Display(); *display = disp; disp->a=11; }

**显示是什么意思? 要访问已使用过的结构体变量-> ,还有其他方法吗?

How can we access variables of a structure? I have a struct:

typedef struct { unsigned short a; unsigned shout b; } Display;

and in my other class I have a method:

int NewMethod(Display **display) { Display *disp=new Display(); *display = disp; disp->a=11; }

What does **display mean? To access variables of struct I have used ->, are there other methods too?

最满意答案

正如泰勒所说,双星号是“指针指针”,您可以根据需要获得尽可能多的指针级别。

我相信你知道,箭头运算符( a->b )是取消引用指针的星号的快捷方式,以及访问字段的点,即

a->b = (*a).b;

括号是必要的,因为点结合更紧密。 对于双星号没有这样的操作符,在访问这些字段之前,必须首先解除引用才能达到所需的级别:

Display **dpl = ...; (*dpl)->a = 42;

要么

(**dpl).a = 42;

As Taylor said, the double asterisk is "pointer to pointer", you can have as many levels of pointers as you need.

As I'm sure you know, the arrow operator (a->b) is a shortcut for the asterisk that dereferences a pointer, and the dot that accesses a field, i.e.

a->b = (*a).b;

The parentheses are necessary since the dot binds tighter. There is no such operator for double asterisks, you have to first de-reference to get to the required level, before accessing the fields:

Display **dpl = ...; (*dpl)->a = 42;

or

(**dpl).a = 42;

更多推荐

本文发布于:2023-07-22 12:23:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1219994.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:变量   结构   Accessing   variables   struct

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!