0

I know that this code:

void incVar(int i){
  i++; 
}

We know this will create a copy of the integer and then increase that value, but not the actual value.

By nature: Methods in C create copies of parameters in their Stack Frame and not the original variable.

But:

 void incVar(int *i){
      (*i)++; 
    }

Is supposed to increase the actual value of the integer by the pointer dereference.

But then, why doesn't C just create a copy of the pointer *i instead? If this is the normal behavior with regular integers, then why doesn't the same thing happen with pointers?

Amad27
  • 151
  • 1
  • 1
  • 8
  • You did not change `i` so that conclusion just comes out of nowhere. – harold Jan 17 '17 at 06:58
  • It is a common mistake to mix up a pointer declaration `int *i` with the pointer de-reference operator `*i`. Stupidly, the C language uses the `*` symbol for many completely different things. – Lundin Jan 17 '17 at 07:46
  • Possible duplicate of [How to understand the pointer star \* in C?](http://stackoverflow.com/questions/5484624/how-to-understand-the-pointer-star-in-c) – Julien Lopez Jan 17 '17 at 08:36

3 Answers3

9

It is the same with pointers. All variables in C are passed by value, even pointers.

You copy the address stored in the pointer outside the function, into its parameter.

But you can use that address to reference a variable which can be allocated anywhere. So in the following code:

int j = 0;
incVar(&j);

incVar receives by-copy the address of j. But it can use that address to read or modify j (in)directly.

StoryTeller - Unslander Monica
  • 165,132
  • 21
  • 377
  • 458
  • I understand. Thanks! So if I have my first method, the address of the copied `x` is different from the global `x` I defined? – Amad27 Jan 17 '17 at 07:00
  • @Amad27 - You mean `i`? Yes, its address is different from the address of the variable that was copied into it. In fact, all valid objects in C have a unique address at any given time. – StoryTeller - Unslander Monica Jan 17 '17 at 07:01
1

In fact the same thing as with values happens with pointers. Just make sure to understand the syntax correctly. You are not passing the integer *i by copy to incVar, but you are passing the pointer i of type int* by copy. No matter how often you copy the pointer to an address, it always points to the same address. So i in your second incVar example points to the integer the caller took the address of. So by derefencing the copy of the pointer (in (*i)++), you are acessing the integer of the caller.

Michael Karcher
  • 3,803
  • 1
  • 14
  • 25
0

Its better to see int *i; as a variable named i of type pointer-to-int, instead of a pointer named *i.

In your example:

 void incVar(int *i) {
      (*i)++; 
    }

We probably use it as such in the main function:

int a = 5;
incVar(&a); //After this line, a is now 6.

What happens in incVar(..) is this:

  1. A variable i is created, of type pointer-to-int.
  2. It holds a copy of the value of &a.
  3. Although i is a copy, its value is the same as that of the value of &a, and it still points to the same integer a.
  4. As a result, by de-referencing i, I refer to the integer a.
  5. Increment the integer a, which obviously is at the address pointed to by i.
Lincoln Cheng
  • 2,263
  • 1
  • 11
  • 17