Value not increasing

Discussion in 'C' started by back from retirement, Nov 26, 2008.

  1. back from retirement

    back from retirement New Member

    Joined:
    Nov 9, 2008
    Messages:
    72
    Likes Received:
    1
    Trophy Points:
    0
    Occupation:
    Student, UG 1st Yr., Jadavpur University
    Location:
    Uttarpara, West Bengal, India
    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;
    [b]num=0;[/b]
    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....
     
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    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.
     
  3. back from retirement

    back from retirement New Member

    Joined:
    Nov 9, 2008
    Messages:
    72
    Likes Received:
    1
    Trophy Points:
    0
    Occupation:
    Student, UG 1st Yr., Jadavpur University
    Location:
    Uttarpara, West Bengal, India
    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... :(
     
  4. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    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.
     
  5. back from retirement

    back from retirement New Member

    Joined:
    Nov 9, 2008
    Messages:
    72
    Likes Received:
    1
    Trophy Points:
    0
    Occupation:
    Student, UG 1st Yr., Jadavpur University
    Location:
    Uttarpara, West Bengal, India
    I see...now I get that point...thanks sir...
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice