我们如何访问一个结构的变量? 我有一个结构:
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;更多推荐
发布评论