Edit where a pointer points within a function

后端 未结 3 1207
温柔的废话
温柔的废话 2021-01-01 01:42

I have a pointer to a struct object in C++.

 node *d=head;

I want to pass that pointer into a function by reference and edit where it point

3条回答
  •  清酒与你
    2021-01-01 02:21

    Pass it by reference:

    void foo(node*& headptr)
    {
         // change it
         headptr = new node("bla");
         // beware of memory leaks :)
    }
    

提交回复
热议问题