1

Why should one change the value of the main variable and NOT the COPY of the variable.

I have a method

int plusInt(int x){
 return ++x; 
}

When this function is called, a new stack frame is created, with the copy of x and not the original variable. So this changes this copy's value?

Q: If I want to change the value of the original variable I use a pointer to it and then increase a value right? Eg:

int plusIntPointer(int *x){
 return ++*x; 
}

But what is the use/why would someone want to change the value of the original variable and not the copy?

1
  • in your example: ++*x be careful about precedence of operators in C. Suggest writing that expression as: ++(*x) Commented Jan 17, 2017 at 16:54

5 Answers 5

5

So this changes this copy's value?

Exactly, as the function has only the copy, and not the original variable, as a local variable.

If I want to change the value of the original variable I use a pointer to it and then increase a value right?

Right again. The reason you use a pointer is to pass the address of the variable to the function.

But what is the use/why would someone want to change the value of the original variable and not the copy?

The reason is that any changes to the copy will be lost after your function ends and you return to the calling function.

For example, let's assume that you want to use a function in order to swap the values of two variables. Then, you have to change the original values. Your function should be like this :

void swap(int *x, int *y)
{
    int temp = *x;
    *x = *y;
    *y = *temp;
}

and you should call it like this :

swap(&a, &b);

This way, the changes will remain even when you return to the calling function. If you just change the copies, the variables will not have swapped values when you return to the calling function!!!

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

1 Comment

Didn't see you added your answer with swap. Have an upvote!
3

Let's say you want to make a function that swaps two variables. Then you need to do this:

void swap(int *a, int *b)
{
  int temp = *a;
  *a = *b;
  *b = *temp;
}
...
void swapwrong(int a, int b)  // wont work as intended
{
  int temp = a;
  a = b;
  b = temp;
}
...
int a = 1, b = 2 ;
swap(&a, &b);
printf ("after swap : a=%d b=%d\n", a,b);
swapwrong(&a, &b);
printf ("after swapwrong : a=%d b=%d\n", a,b);

This will print

after swap : a=2 b=1
after swapwrong : a=2 b=1

Comments

2

So this changes this copy's value?

Yes. Only the copy that is local to plusInt

If I want to change the value of the original variable I use a pointer to it and then increase a value right?

Yes. To change a variable in another scope we must preform an indirection. That is achieved by passing the variables address.

But what is the use/why would someone want to change the value of the original variable and not the copy?

The simplest use case one comes across early while learning to program, is when trying to insert a node into the head of a linked list. Your addition function must modify the structure in a calling context.

Well, maybe this is not the simplest. Consider this function

void swap_ints(int l, int r) {
  int t = l; l = r; r = t;
}

int main(void) {
  int x = 1, y = 2;
  swap_ints(x, y);
  // Were they swapped?
}

Comments

0

But what is the use/why would someone want to change the value of the original variable and not the copy?

You can change the copy variable, but this variable will not be available when you code exits the function.

Also, we don't generally call a function just for doing a simple increment. The code

int plusInt(int x){
 return ++x; 
}

can very well in replace as

x++ 

instead of calling a function. In optimized mode, compilers may decide to get rid of this function by in-lining the change the code wants to do

We use copy variables in many cases, e.g displaying the value of the variable or in the places where you don't want to reflect any accidental changes

Comments

0

There are many applications of the passing parameters to a function by the pointer. E.g.:

When you pass a parameters to a function, as you mentioned, a space in the stack is allocated and such parameters are copied to this space. This operation may be an expensive, so you may consider to pass such parameters by the pointer. Then the pointer to your data will be passed to your function (i.e., will be copied to the stack). In such case you may consider to put the const with such parameters to prevent an "unexpected" changing of their values (as usually done with the char*).

Sometimes you actually need to change a value of such parameter. Pay your attention to that this will be done "in-place". I.e. there will no a copying operations. In the (modified) your example we may to do something like this:

void increment(int* i)
{
  if(i)
    ++*i;
}

In this example there is no any copying operations (as far of the input parameters, as of the result of the function). Moreover, as you can see, we can to pass the NULL as the value of the i. An algorithm of your function may to handle such the case in a different ways (e.g., to skip this parameter etc).

Using of this method of parameter's passing is suggests itself when you need to change a value of many parameters in one function.

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.