简体   繁体   English

在c ++中传递指向函数的指针

[英]Passing pointers to function in c++

Could someone please tell me why are pointers head and tail different when exiting function reverse ? 有人可以告诉我为什么当退出功能reverse时指针headtail不同?

struct elem{
    int val;
    elem* prev;
    elem* next;
    ...
};
void print(elem* head,elem* tail){...}
void insertAtEnd(elem* e,elem* tail){...}
void reverse(elem* head,elem* tail){
    elem* headref = head;
    elem* temp = head;
    while(temp != NULL){
        elem* t = temp->prev;
        temp->prev = temp->next;
        temp->next = t;
        temp = temp->prev;
    }
    head = tail;
    tail = headref;
    print(head,tail);
}
int main(){
    elem* head = new elem();
    elem* tail = new elem();
    ...
    print(head,tail);
    reverse(head,tail);
    print(head,tail);
    return 0;
}

print() inside function reverse works fine. print()里面的函数reverse工作正常。 Next print (just before return 0 in main ) causes segmentation fault ( head->next points to NULL ). 下次print (恰好在main return 0之前)会导致分段错误( head->next指向NULL )。

With void reverse(elem* head,elem* tail) , you don't modify pointer (you may modify content). 使用void reverse(elem* head,elem* tail) ,您不会修改指针(您可以修改内容)。

You probably mean 你可能是说

void reverse(elem*& head, elem*& tail)

to modify head and tail . 修改headtail

The pointers in main() themselves don't change after calling reverse() , because you pass them to reverse() by value, and the code in the function only modifies its own copies of the pointers . main()的指针本身在调用reverse()之后不会更改,因为您按值将它们传递给reverse() ,并且函数中的代码仅修改了其自己的指针副本。 However, the contents of the elem objects they point to have been changed by the reverse() function. 但是,它们指向elem对象的内容已由reverse()函数更改。

That is, head in main() still points to the same elem object it used to point to before, but now that elem object is the tail of the list (because you changed its contents in the reverse() function and now its next member is nullptr ). 也就是说, main() head仍然指向之前指向的同一个elem对象,但现在elem对象是列表的尾部(因为你在reverse()函数中更改了它的内容,现在它的next成员是nullptr )。 Similarly, tail in main() points to an elem that is now the head of the list. 类似地, main() tail指向一个现在是列表头部的elem

Calling reverse doesn't change head and tail because they are passed by value (reverse only modifies its private copies). 调用reverse不会改变head和tail,因为它们是按值传递的(反向只修改其私有副本)。 If you change the declaration of reverse to 如果将反向声明更改为

void reverse(elem *&head, elem *&tail)

It should work. 它应该工作。

Without analysing your code in detail, When you are reversing you are passing pointers to your elements, so the one called head is now the tail and the one called tail is now the head. 在没有详细分析你的代码的情况下,当你正在反转时,你会将指针传递给你的元素,所以名为head的那个现在是尾部,而名为tail的那个现在是头部。

So after you will need to print( tail, head ) 因此,您需要print( tail, head )

Your print function obviously does not check for null that is, it will cause an access violation if next is nullptr before it reaches your tail . 你的print功能显然不检查null ,即如果next到达你的tail之前是nextptr,它将导致访问冲突。 And it will be because head is the tail so its next will indeed be nullptr. 而且因为head是尾,所以它的next确实是nullptr。

If you actually want the reverse function to change the pointers themselves as well as what they point to, so you get a reversed list with the names head and tail swapped, pass them by reference. 如果你真的想要reverse函数来改变指针本身以及它们指向的内容,那么你得到一个颠倒的列表,其名称是headtail交换,通过引用传递它们。

Your exchange of pointers inside reverse() is local. 你在reverse()内部的指针交换是本地的。 It will be discarded once you leave the function scope. 一旦离开功能范围,它将被丢弃。 To achieve the exchange, yo0u have to use double pointers: 要实现交换,yo0u必须使用双指针:

void reverse(elem** head, elem** tail){
    elem* headref = *head;
    elem* temp = *head;
    while(temp != NULL){
        elem* t = temp->prev;
        temp->prev = temp->next;
        temp->next = t;
        temp = temp->prev;
    }
    *head = *tail;
    *tail = headref;
    print(*head,*tail);
}

And pass addresses of head and tail: 并传递头和尾的地址:

reverse(&head, &tail);

PS Sorry for possible bugs - I didn't test the code PS对不起可能的错误 - 我没有测试代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM