473,396 Members | 1,929 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.

Introduction to C language

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
5 1823
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Charlene Russ | last post by:
Learn on-line at your own in a user-centered format with plenty of interaction and personal attention. This is a basic level coursed designed to introduce the novice to intermediate computer...
3
by: Charlene Russ | last post by:
Learn on-line at your own in a user-centered format with plenty of interaction and personal attention. This is a basic level coursed designed to introduce the novice to intermediate computer...
1
by: Xiaoshen Li | last post by:
Dear All, I am relatively new to C(But I have computer science background and know some other programming languages). I am wondering if anyboby can recommend me some C books or websites. I...
2
by: Alex Pavluck | last post by:
Hello. I am considering learning javascript but I am not able to find a decient pdf introduction e-book. Can someone suggest something that would be basic enough for a beginner but useful enough...
15
by: Dave Kelly | last post by:
Does anyone have a recommendation of a good "Introduction to CSS" for an old man who doesn't want to learn a new computer language this late in life but has to if he wants to stay the webadmin of...
29
by: Ralathor | last post by:
Hello everyone. I just wanted to introduce myself, and maby get a few of you... Well, anyway... I am pretty new to programming... At first i looked at the "python" language for a couple...
12
by: Xah Lee | last post by:
Of Interest: Introduction to 3D Graphics Programing http://xahlee.org/3d/index.html Currently, this introduction introduces you to the graphics format of Mathematica, and two Java Applet...
0
by: JosAH | last post by:
Greetings, last week's tip was a bit of playtime where we've built a Sudoku solver. This week we're going to build some complicated stuff: a compiler. Compiler construction is a difficult...
0
Nepomuk
by: Nepomuk | last post by:
Chapter 1: What is an Exception? Imagine, you want to write a program for calculating. You start, it works fine, you can add, subtract, multiply and divide. Then there's a question: What about the...
29
by: Martin Schmied | last post by:
Dear folks I'm looking for good sites/material for this topic: Introduction to programming, using Javascript. So, not introduction to programming *in" Javascript. Well, I guess this would be...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.