“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
"ptr = ptr - > next" What does this "->" mean? (C++)
提问by Robolisk
Possible Duplicate:
What can I use instead of the arrow operator,->
?
What does -> mean in 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->next
is 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 ptr
value will be the memory location of the something
structure (that's what the &
does: finds the memory location of something
). Then the ptr
variable is later "dereferenced" by the ->
operator so that the ptr->bar
memory location (an int) is set to 5 and ptr->baz
is 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->next
it is calling the member variable next
of the object which the pointer block
points 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 block
an 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, ptr
is pointing to a list item so ptr->next
returns the value of the object's next
property.
它正在取消引用指针。它的意思是“给我指向存储在地址的东西的价值ptr
”。在本例中,ptr
指向一个列表项,因此ptr->next
返回对象next
属性的值。