Reputation: 61
I'm a newcomer C learner and following code is my problem:
I wrote it to change the value of the passed variable, but in the output notihng has happened.
Here is my code:
#inlcude <stdio.h>
void change(int i, int j) {
i = i + j;
}
int main() {
int a = 50;
printf("a before = %d\n", a);
change(a, 10);
printf("a after = %d\n", a);
return 0;
}
What is the reason for this error?
Upvotes: 2
Views: 119
Reputation: 1162
This is a very simple case in C.
In C, It uses a method called Call-By-Value to evaluate function parameters. It means when you called a function with your arguments the called function receives the values of those arguments not the arguments.
Which in this program, function "change" receives integer 50 (value the 'a' is holding) and 10 instead of a and 10. So i is 50 and j is 10 when you called the function. In the function i is changed to 60. But a remains the same. If you add a printf() to print the value of i in "change" you can see the change happened.
So to make "change" effective you have to use pointers, a special "tool" in C.
you have to change your "change" function as following:
void change(int *pi, int *pj)
{
*pi += *pj;
}
and you should change the call also.
int main()
{
int a = 50, b = 10;
/* printf line */
change(&a, &b);
/* printf line */
}
in the function "change"
void change(int i, int j)
is changed into
void change(int *pi, int *pj)
this means that pi, pj are pointers to int
. pi
& pj
are variables to hold an address of a variable.
*pi
& *pj
are the variable that resides in the addresses that pi
& pj
have.
Which means that *pi
refers to (points to) a
, *pj
points to b
*pi += *pj
is equal to
a = a + b;
and you can see that I have changed way "change" called
it is now
change(&a, &b);
instead of
change(a, 10);
the ampersand (&) gives the address of a variable So &a and &b are the addresses of a & b, Which are meant to be hold in pi and pj
I think this lengthy answer would help you. However you should read some lessons about pointers.
Upvotes: 1
Reputation: 310910
Function parameters are its local variables. Function parameters are initialized by copies of the values of the supplied arguments. So any changing of a parameter does not influence on the corresponding argument. After exiting the function its parameters (local variables) are destroyed.
For example you can imagine your function
void change(int i, int j) {
i = i + j;
}
that is called like
change(a, 10);
the following way
void change(/*int i, int j*/) {
int i = a;
int j = 10;
i = i + j;
}
As you can see variable a itself will not be changed.
There are two approaches. Either the first parameter will be passed by reference or the function will return its result. For example
#inlcude <stdio.h>
void change( int *i, int j) {
*i = *i + j;
}
int main() {
int a = 50;
printf("a before = %d\n", a);
change( &a, 10);
printf("a after = %d\n", a);
return 0;
}
Or
#inlcude <stdio.h>
int change(int i, int j) {
i = i + j;
return i;
}
int main() {
int a = 50;
printf("a before = %d\n", a);
a = change(a, 10);
printf("a after = %d\n", a);
return 0;
}
Upvotes: 4
Reputation: 338
The variable i
and j
in change()
are copies from the variables inside main()
. You have to learn about pointers to achieve what you are trying to do. Basically it look like this:
#include <stdio.h>
void change(int *i, int j) {
*i = *i + j;
}
int main() {
int a = 50;
printf("a before = %d\n", a);
change(&a, 10);
printf("a after = %d\n", a);
return 0;
}
Upvotes: 1