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"