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?

3
  • You did not change i so that conclusion just comes out of nowhere. Commented Jan 17, 2017 at 6: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. Commented Jan 17, 2017 at 7:46
  • Possible duplicate of How to understand the pointer star * in C? Commented Jan 17, 2017 at 8:36

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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 - 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.
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.

Comments

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.