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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9595361/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 13:02:19  来源:igfitidea点击:

"ptr = ptr - > next" What does this "->" mean? (C++)

c++pointerslinked-list

提问by Robolisk

Possible Duplicate:
What can I use instead of the arrow operator, ->?
What does -> mean in C++?

可能的重复:
我可以用什么代替箭头运算符,->
-> 在 C++ 中是什么意思?

So I'm currently studying for a C++ exam about Data Structure and Algorithm Development. While looking though my teacher powerpoints, I've noticed that he's used this "->" a lot. And I'm not sure what it means? Is it really a command you can do in c++?

所以我目前正在学习关于数据结构和算法开发的 C++ 考试。在查看我老师的幻灯片时,我注意到他经常使用“->”。我不确定这意味着什么?它真的是你可以在 C++ 中执行的命令吗?

Example 1

示例 1

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

Example 2

示例 2

if( head == NULL )
{
head = block;
block->next = NULL;
}

回答by Gordon Bailey

It is a combination dereference and member-access. This ptr->nextis equivalent to (*ptr).next.

它是取消引用和成员访问的组合。这ptr->next相当于(*ptr).next.

回答by Wes Hardaker

The ->operator deferences a pointer and retrieves from it the memory index beyond that point indicated by the following name. Thus:

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

struct foo {
   int bar;
   int baz;
};

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

ptr->bar = 5;
ptr->baz = 10;

In the above, the ptrvalue will be the memory location of the somethingstructure (that's what the &does: finds the memory location of something). Then the ptrvariable is later "dereferenced" by the ->operator so that the ptr->barmemory location (an int) is set to 5 and ptr->bazis set to 10.

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

回答by rob05c

The ->operator is specifically a structure dereference. In block->nextit is calling the member variable nextof the object which the pointer blockpoints to. See thispage for a list of member and pointer operators in C++.

->操作者具体为结构解除引用。在block->next它调用next指针block指向的对象的成员变量。有关C++ 中成员和指针运算符的列表,请参阅页面。

Basically, it's doing the same thing as block.next, were blockan object rather than a pointer.

基本上,它的作用与block.next, 是block一个对象而不是一个指针。

回答by Timothy Ye

-> is an operator of pointer. It is used for pointer to access member.

-> 是指针的运算符。它用于访问成员的指针。

If you take a look at the defination of "addrInfo", you can find the member "next".

如果查看“addrInfo”的定义,可以找到成员“next”。

Otherwise, you can see the following example:

否则,您可以看到以下示例:

struct student
{
int num;
}
struct student stu;
struct student *p;
p=&stu;

These three operations are equal: 1. stu.num 2. (*P).num 3. p->num

这三个操作是相等的: 1. stu.num 2. (*P).num 3. p->num

回答by D Stanley

It's de-referencing the pointer. It means "Give me the value of the thing pointed at the address stored at ptr". In this example, ptris pointing to a list item so ptr->nextreturns the value of the object's nextproperty.

它正在取消引用指针。它的意思是“给我指向存储在地址的东西的价值ptr”。在本例中,ptr指向一个列表项,因此ptr->next返回对象next属性的值。