1

So I am trying to understand a simple piece of code that 'emulates' Pass-by-reference using pointers in a Pass by value function. The example is given as a C practice since there is no Pass by reference in C. but I am also curious about its effects on C++.

so after running this code the values are swapped:

void swap(int* a, int* b)
{
  int temp;
  temp = *a;
  *a = *b;
  *b = temp;
}

I am trying to understand why passing pointers as arguments create a pass-by-reference effect? Does it mean all actions that are performed through pointers have a pass-by-reference effect? So the swapped memory location will not go back once we quit the function? Would this also be valid in C++?

New to coding so I hope I could make myself clear.

Luc Aux
  • 149
  • 10
  • What C textbook are you learning from? And when you are asking questions about functions, post the code for the function call, as well as for the function itself. –  Sep 29 '17 at 23:38
  • Okay I guess I am clearly ahead of myself. I'll edit the question. – Luc Aux Sep 29 '17 at 23:39
  • Yes to all of the questions. – Petr Skocik Sep 29 '17 at 23:42
  • 1
    A pointer is a type of reference, so pass by pointer is still pass by reference. – user4581301 Sep 29 '17 at 23:45
  • From your edit, it sounds like you need a [solid textbook](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) or at the very least, a good tutorial on pointers. – scohe001 Sep 29 '17 at 23:45
  • Thank you. I got the answers I needed. Yes I was a little ahead of myself with the question. I have just started looking into pointers. – Luc Aux Sep 29 '17 at 23:47
  • 1
    One note worth bringing up. While the item pointed at is passed by reference, remember that the pointer itself is not. Sometimes you will want to change where a pointer points inside a function and you will have to pass a reference to the pointer itself. `int *& a` or `int ** a` – user4581301 Sep 29 '17 at 23:58
  • A `*` after a typename declares a pointer of that type. A `*` in front of a pointer variable dereferences the pointer. – Remy Lebeau Sep 30 '17 at 00:17
  • If you want to learn C++, just do not look at C, ignore it. – alk Sep 30 '17 at 09:23

2 Answers2

5

Basically, your code is copying a memory address to the function, while if you omitted the * operator, the function would be copying what's in a memory address to the function. Here's what each line is essentially saying.

// Give me the memory address of a and b.
void swap(int* a, int* b)
{
  int temp;
  // Whatever value is at memory address 'a', copy it to temp.
  temp = *a; 
  // Whatever value is at memory address 'b', copy it to memory address 'a'.
  *a = *b;
  // Copy temp over the value at memory address 'b'.
  *b = temp;
}

And yes, it would be equally valid in C++.

John Stritenberger
  • 1,194
  • 1
  • 12
  • 30
1

You can basically think of pointers and references as the same thing. They both refer to addresses. So if you pass an address (either as pointer or as reference) to a function, and the content of the address changes, then the change is still there at the end of the function.

user835611
  • 2,309
  • 2
  • 21
  • 29