Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Incrementing in C

Reply
Thread Tools

Incrementing in C

 
 
jobo
Guest
Posts: n/a
 
      11-01-2006
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!

 
Reply With Quote
 
 
 
 
T.M. Sommers
Guest
Posts: n/a
 
      11-01-2006
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 -- http://www.velocityreviews.com/forums/(E-Mail Removed) -- AB2SB

 
Reply With Quote
 
 
 
 
Chris Dollin
Guest
Posts: n/a
 
      11-01-2006
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/

 
Reply With Quote
 
=?ISO-8859-1?Q?=22Nils_O=2E_Sel=E5sdal=22?=
Guest
Posts: n/a
 
      11-01-2006
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Incrementing value test Salman VHDL 2 04-13-2005 04:52 PM
Incrementing Assembly Version? =?Utf-8?B?bWVraW0=?= ASP .Net 5 10-29-2004 01:08 PM
Incrementing VHDL FOR loop constant by a value other than 1 Josh Graham VHDL 3 04-07-2004 07:03 AM
looping/incrementing problem... Mothra Perl 3 12-10-2003 02:01 AM
Incrementing Datagrid Value -- Simple Question Ron ASP .Net 0 06-30-2003 04:37 PM



Advertisments