Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   Value not increasing (http://www.go4expert.com/forums/value-not-increasing-t15263/)

back from retirement 26Nov2008 19:16

Value not increasing
 
I was trying to increase the value of a variable within a loop according to my own wish...
e.g. initially the variable value is declared 0. Now within the loop I keep a parameter n, the value of which will be entered by me. say first time I enter n=6. Then the value 0 becomes 6. If I enter 5 next, the valu will become 11.

But practically, due to error in position of declaration of the initial value of the variable, after each iteration, variable value is becoming zero again...

Here is how far I came...

Code:

#include<stdio.h>
#include<conio.h>

int inc(int num, int n)
{
num=num+n;
return(num);
}

void main()
{
int num,n,i;
num=0;
for(i=0;i<20;i++)
{
printf("\nEnter n=");
scanf("%d", &n);
printf("\nNew value of num=%d\n", inc(num,n));
}
getch();
}

The bold line is the declaration position error I am committing...but I am unable to fix it...
Please help...thanks in advance...

Cheers....

xpi0t0s 26Nov2008 19:28

Re: Value not increasing
 
In C parameters are passed by value, not reference. num=num+n updates the *local copy* of num, not the num in main(). Let's change the variable for clarity:
Code:

int inc(int foo, int n)
{
foo+=n;
return(foo);
}

and main remains the same. foo is updated by this function, but num is not.

back from retirement 26Nov2008 19:43

Re: Value not increasing
 
Code:

foo+=n;
and
Code:

num=num+n;
both are same declarations....at least according to my compiler, for the results remain the same, it is updating the local copy of num again... :(

Please tell me, how would I interpret the main??? And where will I declare the initial value of num in main?? Outside or inside the loop iterates the same results... :(

xpi0t0s 26Nov2008 20:44

Re: Value not increasing
 
What you want here is what's wrong in the other thread... :-)

What's confusing you in both cases is the difference between passing by value and passing by reference.

C doesn't do passing by reference, which is where the function modifies the variable in the calling function. It only does passing by value, and if you update the value locally, only the local copy is changed and the copy in the calling function is not. So if you want to modify the variable in the calling function, which is num in your case, then you need to take the address of num and pass the address in:

Code:

int inc(int *foo,int n)
{
  *foo += n;
  return *foo;
}

So foo here is a pointer to int, and to modify num in main, you _dereference_ the pointer you've been passed in. foo is a pointer to num, and *foo is equivalent to using num directly.

In main you have to take the address of num and pass it into inc:

Code:

printf("\nNew value of num=%d\n", inc(&num,n));
In the other thread you are passing in a pointer to top and using it to modify top directly. Since top is the only reference to the top of the stack, when you've used the pointer to top dereferenced, i.e. top itself, to walk the list and display the values, you no longer have anything pointing to the top of the stack.

back from retirement 26Nov2008 20:46

Re: Value not increasing
 
I see...now I get that point...thanks sir...


All times are GMT +5.5. The time now is 18:12.