By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,341 Members | 1,396 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,341 IT Pros & Developers. It's quick & easy.

Introduction to C language

P: n/a
void swap(int *p,*q)
{
int t;
t=*p;
*p=*q;
*q=t;
}
coresponding call statement
x=4;
y=5;
swap(&x,&y);
this function does swaping but
if we write this swap function in call by value then swapping is not
done why?

Sep 22 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
void swap(int *p,*q)
{
int t;
t=*p;
*p=*q;
*q=t;
}
This function will take the values of p and q as addresses of the
values to be swapped. And these addresses in the memory space is a 2
byte integer.
coresponding call statement
x=4;
y=5;
swap(&x,&y);
this function does swaping but
if we write this swap function in call by value then swapping is not
done why?
That is why this statement will not given an error; on the other hand,
it will locate address locations 4 and 5 and see the addresses
corresponding to the contents in their cell would have swapped values.

Sep 22 '07 #2

P: n/a
On Sat, 22 Sep 2007 19:11:13 -0000, Vivek
<vi*****************@gmail.comwrote in comp.lang.c:
void swap(int *p,*q)
{
int t;
t=*p;
*p=*q;
*q=t;
}

This function will take the values of p and q as addresses of the
values to be swapped. And these addresses in the memory space is a 2
byte integer.
What causes you to make this mostly incorrect statement. On the two C
implementations that I use most these days, an int occupies one byte
in one of them, and four bytes in the other.
coresponding call statement
x=4;
y=5;
swap(&x,&y);
this function does swaping but
if we write this swap function in call by value then swapping is not
done why?

That is why this statement will not given an error; on the other hand,
it will locate address locations 4 and 5 and see the addresses
corresponding to the contents in their cell would have swapped values.
Perhaps English is not your native language and there is a language
problem here, but I find that your reply makes no sense at all.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
Sep 22 '07 #3

P: n/a
piyush <pi**********@gmail.comwrites:
void swap(int *p,*q)
This is a syntax error. The correct declaration is:

void swap(int *p, int *q)

Please compile your code before posting it, and copy-and-paste the
exact code that you fed to the compiler. It's not a big deal in this
case, but by posting code that you write on the fly without testing
it, you can easily introduce and/or hide subtle errors.
{
int t;
t=*p;
*p=*q;
*q=t;
}
coresponding call statement
x=4;
y=5;
swap(&x,&y);
this function does swaping but
Right.
if we write this swap function in call by value then swapping is not
done why?
All argument passing in C is by value. You can implement or simulate
call-by-reference by passing pointers, as you've done here, but the
pointers themselves are always passed by value.

It would have been helpful to provide actual code for your "call by
value" swap routine. Here's what I think you meant:

void no_swap(int p, int q)
{
int t = p;
p = q;
q = t;
}
/* ... */
int x = 4;
int y = 5;
no_swap(x, y);

This doesn't swap the values of x and y because they're passed by
value. What the no_swap function sees is *copies* of the *values* of
x and y; it has no visibility at all to the *objects* (variables)
themselves, so it can't affect them.

Within no_swap, p and q, the parameters, are local variables; they
cease to exist as soon as the function finishes, and their values are
quietly discarded.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Sep 22 '07 #4

P: n/a
Vivek <vi*****************@gmail.comwrites:
>void swap(int *p,*q)
{
int t;
t=*p;
*p=*q;
*q=t;
}

This function will take the values of p and q as addresses of the
values to be swapped. And these addresses in the memory space is a 2
byte integer.
The size of an object of type 'int' is 'sizeof(int)'. It may be two
bytes on some systems, but I've seen 4 and even 8 bytes.

The size of an address also varies from one system to another, and
addresses are *not* integers.
>coresponding call statement
x=4;
y=5;
swap(&x,&y);
>this function does swaping but
if we write this swap function in call by value then swapping is not
done why?

That is why this statement will not given an error; on the other hand,
it will locate address locations 4 and 5 and see the addresses
corresponding to the contents in their cell would have swapped values.
Um, no, nothing in the code above refers to memory addresses 4 and 5.
x and y are objects of type int whose *values* are 4 and 5. We have
no idea where in memory those objects are stored, but the program can
find out by referring to '&x' and '&y'.

If the swap() function, which takes two pointer arguments, were called
as 'swap(x, y)' or 'swap(4, 5)', it would be an error; you can't pass
an integer value to something that expects a pointer. (There's one
exception to this, involving null pointers).

I suggest you take a look at the comp.lang.c FAQ,
<http://www.c-faq.com/>.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Sep 22 '07 #5

P: n/a
On 9 23 , 12 02 , piyush <piyushcsi...@gmail.comwrote:
void swap(int *p,*q)
{
int t;
t=*p;
*p=*q;
*q=t;}

coresponding call statement
x=4;
y=5;
swap(&x,&y);
this function does swaping but
if we write this swap function in call by value then swapping is not
done why?
Because the swapping executes just in the function swap(). When the
function ends, the environment made by the function ends too. But the
variables in the outside environment (Calling function environment)
have not swapped.

Sep 23 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.