"ptr = ptr - > next"这是什么" - >"是什么意思?(C++)

Rob*_*isk 2 c++ pointers linked-list

可能重复:
我可以使用什么代替箭头操作符->
什么 - >在C++中意味着什么?

所以我目前正在攻读有关数据结构和算法开发的C++考试.虽然看着我的老师powerpoint,但我注意到他经常使用这个" - >".我不确定这意味着什么?它真的是一个你可以用c ++做的命令吗?

例1

addrInfo *ptr = head;
while (ptr->next != NULL) 
{
        ptr = ptr->next;
}   
// at this point, ptr points to the last item
Run Code Online (Sandbox Code Playgroud)

例2

if( head == NULL )
{
head = block;
block->next = NULL;
}
Run Code Online (Sandbox Code Playgroud)

Gor*_*ley 7

它是一个组合解除引用和成员访问.这ptr->next相当于(*ptr).next.


Wes*_*ker 5

所述->操作者deferences一个指针和检索从它的存储器索引超出由下列名称表示了这一点。因此:

struct foo {
   int bar;
   int baz;
};

struct foo something;
struct foo *ptr = &something;

ptr->bar = 5;
ptr->baz = 10;
Run Code Online (Sandbox Code Playgroud)

在上面,该ptr值将是something结构的内存位置(这就是它的&作用:找到 的内存位置something)。然后该ptr变量稍后由->操作员“取消引用”,以便将ptr->bar内存位置(一个 int)设置为 5 并ptr->baz设置为 10。