Incrementing in C

Discussion in 'C Programming' started by jobo, Nov 1, 2006.

  1. jobo

    jobo Guest

    Suppose I have a function foo:

    void foo(int x, int y) {

    printf(" %d ", y);
    x = y;


    }

    I have two ints a and b.
    a = 7;
    b = 7;
    then I call foo(a, ++b);

    For some reason at the end of running foo, I get a = 7 and b = 8.

    Why do I not have a = b = 8?

    Thank you much!
     
    jobo, Nov 1, 2006
    #1
    1. Advertising

  2. jobo

    T.M. Sommers Guest

    jobo wrote:
    > Suppose I have a function foo:
    >
    > void foo(int x, int y) {
    >
    > printf(" %d ", y);
    > x = y;
    >
    >
    > }
    >
    > I have two ints a and b.
    > a = 7;
    > b = 7;
    > then I call foo(a, ++b);
    >
    > For some reason at the end of running foo, I get a = 7 and b = 8.
    >
    > Why do I not have a = b = 8?


    Because x is local to the function foo. In C function arguments
    are passed by value, not by reference. If you want a function to
    change an object in the calling function, you must pass in a pointer:

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

    and call it:

    int a, b;
    ....
    foo(&a, b);

    --
    Thomas M. Sommers -- -- AB2SB
     
    T.M. Sommers, Nov 1, 2006
    #2
    1. Advertising

  3. jobo

    Chris Dollin Guest

    jobo wrote:

    > Suppose I have a function foo:
    >
    > void foo(int x, int y) {
    >
    > printf(" %d ", y);
    > x = y;
    > }


    The assignment of `y` to `x` is pointless; `x` is a local
    variable that will evaporate when `foo` returns.

    > I have two ints a and b.
    > a = 7;
    > b = 7;
    > then I call foo(a, ++b);
    >
    > For some reason at the end of running foo, I get a = 7 and b = 8.


    Well, yes. You assigned `7` to `a` and `b` and incremented `b`.
    Calling `foo` doesn't change that.

    > Why do I not have a = b = 8?


    Because the assignment of `y` to `x` is pointless; `x` is a local
    variable that will evaporate when `foo` returns.

    --
    Chris "echo echo" Dollin
    "Never ask that question!" Ambassador Kosh, /Babylon 5/
     
    Chris Dollin, Nov 1, 2006
    #3
  4. jobo wrote:
    > Suppose I have a function foo:
    >
    > void foo(int x, int y) {
    >
    > printf(" %d ", y);
    > x = y;
    >
    >
    > }
    >
    > I have two ints a and b.
    > a = 7;
    > b = 7;
    > then I call foo(a, ++b);
    >
    > For some reason at the end of running foo, I get a = 7 and b = 8.
    >
    > Why do I not have a = b = 8?


    C passes values. When you call 'foo', the values in your a and b
    variables are "copied" to the variables x and y. After that there's no
    relation between a,b and x,y.
     
    =?ISO-8859-1?Q?=22Nils_O=2E_Sel=E5sdal=22?=, Nov 1, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Josh Graham
    Replies:
    3
    Views:
    23,404
    Nicolas Matringe
    Apr 7, 2004
  2. Salman

    Incrementing value test

    Salman, Apr 13, 2005, in forum: VHDL
    Replies:
    2
    Views:
    651
    Engineering Guy
    Apr 13, 2005
  3. Mothra
    Replies:
    3
    Views:
    449
  4. Ron
    Replies:
    0
    Views:
    321
  5. =?Utf-8?B?bWVraW0=?=

    Incrementing Assembly Version?

    =?Utf-8?B?bWVraW0=?=, Oct 28, 2004, in forum: ASP .Net
    Replies:
    5
    Views:
    7,375
    Saravana
    Oct 29, 2004
Loading...

Share This Page