-1

Apologies if this question has been answered elsewhere, I wasn't sure exactly how to word a search for this.

My question is if I pass a pointer to a function, does that function get a copy of the pointer or the actual pointer that was passed? in other words, if I have

printNext(myClass* foo)
{
    foo = foo->next;
    cout << foo;
}

will foo be changed permanently or just temporarily in the scope of the function?

if it is changed permanently, how do I make it not do that? should I use printNext(myClass*& foo) ?

Jackson
  • 47
  • 7

1 Answers1

1

You are sending a copy of the pointer to printNext. Any changes to the parameter will change the copy and not the original.

If you want to change the pointer, either pass by reference or pass a pointer to the pointer.

Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154
  • perfect, thanks! do you know any good free resources that go in depth about pointers and this kind of thing? – Jackson Apr 12 '18 at 15:38
  • @Jackson consider a good C++ book: https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – SergeyA Apr 12 '18 at 15:52