473,396 Members | 1,757 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Incrementing in C

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!

Nov 1 '06 #1
3 1448
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 -- tm*@nj.net -- AB2SB

Nov 1 '06 #2
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/

Nov 1 '06 #3
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.
Nov 1 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Tom | last post by:
Anyone help on this? PHP/MySQL I have a repeat region displaying records from orders in a CSV format: CalJoe33,18,08/23/2004,FED EX,PREPAID,WEBSITE,CA,Book,25.95,??? CalJoe33,18,08/23/2004,FED...
3
by: Mothra | last post by:
Here's what I'm trying to do (kill off old Unix logins): --------------------- $i=0; while (<$who>) { chomp($_); my @line = split(/\s+/, $_); # Split it into an array next unless ($line...
2
by: Mike Brearley | last post by:
I have a counter (alright one I found on asp101.com) that checks for a session variable to prevent the counter from incrmenting if a user refreshes the page or returns to the page during the same...
6
by: J. Campbell | last post by:
Hi everyone. I'm sure that this is common knowledge for many/most here. However, it was rather illuminating for me (someone learning c++) and I thought it might be helpful for someone else. I'm...
2
by: brian | last post by:
Hi, before coming to .NET, I utilized regular expressions mostly in JScript / JavaScript and also in my favorite text editor: TextPad (www.textpad.com) I don't know about JScript/JavaScript, but...
10
by: Antanas | last post by:
The problem is that when AddID is used multiple times in the same select statement, it returns the same value in all places. How could I force function AddID to increment OBJECTID sequence? Here...
2
by: Payson Books | last post by:
Where can I find some info on grabbing the recordID of the last record, incrementing it by one, and using the new number in a new record.
26
by: Bas Wassink | last post by:
Hi there, Does the ANSI standard say anything about incrementing variables past their limits ? When I compile code like this: unsigned char x = 255; x++; printf ( "%d\n", x );
53
by: subramanian100in | last post by:
I saw this question from www.brainbench.com void *ptr; myStruct myArray; ptr = myArray; Which of the following is the correct way to increment the variable "ptr"? Choice 1 ptr = ptr +...
9
by: subramanian100in | last post by:
The following portion is from c-faq.com - comp.lang.c FAQ list · Question 6.13 int a1 = {0, 1, 2}; int a2 = {{3, 4, 5}, {6, 7, 8}}; int *ip; /* pointer to int */ int (*ap); /* pointer to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.