Skip to main content
added 250 characters in body
Source Link

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?
}

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.

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?
}
Source Link

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.