469,363 Members | 2,560 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,363 developers. It's quick & easy.

C : Call by value or reference

Does C follow call by value convention or call by reference?
i see that there is nothing like reference in C standard but it is
referenced.

still, what should be the answer for the above question?

Jul 15 '07 #1
22 3968
In article <11**********************@g4g2000hsf.googlegroups. com>,
Nehil <ne***********@gmail.comwrote:
>Does C follow call by value convention or call by reference?
i see that there is nothing like reference in C standard but it is
referenced.

still, what should be the answer for the above question?
Are you the same "Nehil" who is undertaking to write
a garbage collector for C (*)? If so, have you ever heard the
advice "Walk before you run?"

(*) And, no doubt, next week, the Mars lander program.

Jul 15 '07 #2
On Jul 15, 6:00 pm, gaze...@xmission.xmission.com (Kenny McCormack)
wrote:
In article <1184504183.456641.240...@g4g2000hsf.googlegroups. com>,

Nehil <nehilparas...@gmail.comwrote:
Does C follow call by value convention or call by reference?
i see that there is nothing like reference in C standard but it is
referenced.
still, what should be the answer for the above question?

Are you the same "Nehil" who is undertaking to write
a garbage collector for C (*)? If so, have you ever heard the
advice "Walk before you run?"

(*) And, no doubt, next week, the Mars lander program.
have u changed your name or r u same as Eric Sosman?
well, what if i want to learn the new concepts of much debate. and by
now the garbage collector is almost done as a low lovel project. and
all the things i've asked are related to this project only somehow.
so if u know the answer plz reply appropriately.

Jul 15 '07 #3
Nehil said:
Does C follow call by value convention or call by reference?
C is a 100% call-by-value language. Function arguments are expressions,
not objects. These expressions are evaluated, and those values are
passed to the function.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jul 15 '07 #4
Nehil wrote:
Does C follow call by value convention or call by reference?
Call by value.

If one wants the effects provided by call-by-reference, one has to
implement them by hand.

[Note that for /arrays/, it can /look like/ call by reference;
but that isn't anything to do with parameter-passing, it's
to do with the way C arrays decay into pointers.]
i see that there is nothing like reference in C standard but it is
referenced.
Pardon?
still, what should be the answer for the above question?
I don't know what the answer /should/ be; I only know what it /is/.

--
Passed By Name Hedgehog
"Our future looks secure, but it's all out of our hands"
- Magenta, /Man and Machine/

Jul 15 '07 #5
Nehil wrote:
Does C follow call by value convention or call by reference?
i see that there is nothing like reference in C standard but it is
referenced.

still, what should be the answer for the above question?
C only supports call by value. However you can simulate call by
reference using pointers.

Jul 15 '07 #6
On Sun, 15 Jul 2007 13:22:52 +0000, Richard Heathfield wrote:
>C is a 100% call-by-value language. Function arguments are expressions,
not objects. These expressions are evaluated, and those values are
passed to the function.
This statement is not wrong but highly misleading. Esp. it doesn't
cover the distinction between the following:

void foo1 (stuct something sth);
void foo2 (stuct something* ptr);
--
Roland Pibinger
"The best software is simple, elegant, and full of drama" - Grady Booch
Jul 15 '07 #7
santosh <sa*********@gmail.comwrites:
Nehil wrote:
>Does C follow call by value convention or call by reference?
i see that there is nothing like reference in C standard but it is
referenced.

still, what should be the answer for the above question?

C only supports call by value. However you can simulate call by
reference using pointers.
and arrays...

http://mces.blogspot.com/2005/08/pas...ence-in-c.html
Jul 15 '07 #8
rp*****@yahoo.com (Roland Pibinger) writes:
On Sun, 15 Jul 2007 13:22:52 +0000, Richard Heathfield wrote:
>>C is a 100% call-by-value language. Function arguments are expressions,
not objects. These expressions are evaluated, and those values are
passed to the function.

This statement is not wrong but highly misleading. Esp. it doesn't
cover the distinction between the following:

void foo1 (stuct something sth);
void foo2 (stuct something* ptr);
It's all "words". I suspect the OP was looking for pointers - which also
makes me wonder if he is a troll. Since how can he implement a garbage
collector and not either (a) know the answer or (b) know how to google
up the answer.
Jul 15 '07 #9
Roland Pibinger wrote:
On Sun, 15 Jul 2007 13:22:52 +0000, Richard Heathfield wrote:
>>C is a 100% call-by-value language. Function arguments are expressions,
not objects. These expressions are evaluated, and those values are
passed to the function.

This statement is not wrong but highly misleading. Esp. it doesn't
cover the distinction between the following:

void foo1 (stuct something sth);
void foo2 (stuct something* ptr);
Two functions, distinguished by argument types only. So what?
Structs are passed by value, and pointers are passed by value.
What distinction are you referring to?

--
Multiple Valued Hedgehog
Nit-picking is best done among friends.

Jul 15 '07 #10
In article <6I*******************@fe1.news.blueyonder.co.uk >,
Chris Dollin <eh@electrichedgehog.netwrote:
>Roland Pibinger wrote:
>On Sun, 15 Jul 2007 13:22:52 +0000, Richard Heathfield wrote:
>>>C is a 100% call-by-value language. Function arguments are expressions,
not objects. These expressions are evaluated, and those values are
passed to the function.

This statement is not wrong but highly misleading. Esp. it doesn't
cover the distinction between the following:

void foo1 (stuct something sth);
void foo2 (stuct something* ptr);

Two functions, distinguished by argument types only. So what?
Structs are passed by value, and pointers are passed by value.
What distinction are you referring to?
Yes, we all get it. But apparently, Nillios doesn't.

The fact is that there is a class of programmers out there, programming
in C, who don't really understand the language. They neither know nor
care about the distinction between pointers being passed by value (which
is part of C) and variables being called by reference (which is not part
of C). They assume that they are equivalent concepts (which, to a first
approximation, they are) and that, thus C does support call by reference.

Jul 15 '07 #11
Chris Dollin <eh@electrichedgehog.netwrites:
Roland Pibinger wrote:
>On Sun, 15 Jul 2007 13:22:52 +0000, Richard Heathfield wrote:
>>>C is a 100% call-by-value language. Function arguments are expressions,
not objects. These expressions are evaluated, and those values are
passed to the function.

This statement is not wrong but highly misleading. Esp. it doesn't
cover the distinction between the following:

void foo1 (stuct something sth);
void foo2 (stuct something* ptr);

Two functions, distinguished by argument types only. So what?
Structs are passed by value, and pointers are passed by value.
What distinction are you referring to?
Because 99% of the time that C nOObs ask this question they are
enquiring how to pass a pointer to a large object as opposed to the
entire variable.
Jul 15 '07 #12
ga*****@xmission.xmission.com (Kenny McCormack) writes:
In article <6I*******************@fe1.news.blueyonder.co.uk >,
Chris Dollin <eh@electrichedgehog.netwrote:
>>Roland Pibinger wrote:
>>On Sun, 15 Jul 2007 13:22:52 +0000, Richard Heathfield wrote:
C is a 100% call-by-value language. Function arguments are expressions,
not objects. These expressions are evaluated, and those values are
passed to the function.

This statement is not wrong but highly misleading. Esp. it doesn't
cover the distinction between the following:

void foo1 (stuct something sth);
void foo2 (stuct something* ptr);

Two functions, distinguished by argument types only. So what?
Structs are passed by value, and pointers are passed by value.
What distinction are you referring to?

Yes, we all get it. But apparently, Nillios doesn't.

The fact is that there is a class of programmers out there, programming
in C, who don't really understand the language. They neither know nor
care about the distinction between pointers being passed by value (which
is part of C) and variables being called by reference (which is not
part
And, in my experience never, ever have any problems with this in the C
context.

of C). They assume that they are equivalent concepts (which, to a first
approximation, they are) and that, thus C does support call by reference.
--
Jul 15 '07 #13
On Jul 15, 10:05 pm, Richard <rgr...@gmail.comwrote:
rpbg...@yahoo.com (Roland Pibinger) writes:
On Sun, 15 Jul 2007 13:22:52 +0000, Richard Heathfield wrote:
>C is a 100% call-by-value language. Function arguments are expressions,
not objects. These expressions are evaluated, and those values are
passed to the function.
This statement is not wrong but highly misleading. Esp. it doesn't
cover the distinction between the following:
void foo1 (stuct something sth);
void foo2 (stuct something* ptr);

It's all "words". I suspect the OP was looking for pointers - which also
makes me wonder if he is a troll. Since how can he implement a garbage
collector and not either (a) know the answer or (b) know how to google
up the answer.
Both your declarations follow pass by value. What is there in it?

Many poorly written C books call "simulation of pass by reference
using pointers" as pass by reference which confuses a lot of Newbies.
I feel that Call by reference is said to be supported by a language IF
it syntactically and semantically supports that facility. C doesn't
have any syntactical and semantical facility for Call by Reference.
People find it difficult to understand that simple concept and call
"simulation of pass by reference using pointers" as REAL pass by
reference!

Jul 15 '07 #14
On Sun, 15 Jul 2007 13:06:04 -0000, Nehil <ne***********@gmail.com>
wrote:
>On Jul 15, 6:00 pm, gaze...@xmission.xmission.com (Kenny McCormack)
wrote:
>In article <1184504183.456641.240...@g4g2000hsf.googlegroups. com>,

Nehil <nehilparas...@gmail.comwrote:
>Does C follow call by value convention or call by reference?
i see that there is nothing like reference in C standard but it is
referenced.
>still, what should be the answer for the above question?

Are you the same "Nehil" who is undertaking to write
a garbage collector for C (*)? If so, have you ever heard the
advice "Walk before you run?"

(*) And, no doubt, next week, the Mars lander program.

have u changed your name or r u same as Eric Sosman?
well, what if i want to learn the new concepts of much debate. and by
now the garbage collector is almost done as a low lovel project. and
all the things i've asked are related to this project only somehow.
so if u know the answer plz reply appropriately.
You seem to be of the opinion that this group is supposed to provide
you with answers to even the most rudimentary questions while you make
no effort to research the answers yourself. You don't have to take my
word for it but I assure this opinion is held by very few participants
and they are not the ones who will help you because they are just as
lazy as you.

If you don't have a C reference, get one. Go to google (you should be
able to find it since you use their email) and browse the archives for
this group. Check the faq. These issues you call debatable have been
addressed repeatedly.

In several posts you have implied you are attempting rather complex
projects. In others you have demonstrated that you are woefully
unqualified for them. Complex tasks require firm foundations. Infants
learn to crawl before they walk, walk before they run, and in all
cases to pick themselves up after falling. If you don't want to be
considered a troll, don't jump off the high diving board before you
learn to swim.
Remove del for email
Jul 15 '07 #15
On Sun, 15 Jul 2007 17:56:12 +0000, CryptiqueGuy wrote:
On Sun, 15 Jul 2007 13:22:52 +0000, Richard Heathfield wrote:
>>C is a 100% call-by-value language. Function arguments are
expressions,
not objects. These expressions are evaluated, and those values are
passed to the function.
This statement is not wrong but highly misleading. Esp. it doesn't
cover the distinction between the following:
void foo1 (stuct something sth);
void foo2 (stuct something* ptr);
^^^^^
What happened to 'r'? :-)
>It's all "words". I suspect the OP was looking for pointers - which
also makes me wonder if he is a troll. Since how can he implement a
garbage collector and not either (a) know the answer or (b) know how to
google up the answer.

Both your declarations follow pass by value. What is there in it?
Absolutely nothing.
But if foo2 is called as foo2(&someth) and its definition dereferences
ptr, foo2 is able to access a variable local to its caller, which foo1
will never be able to do (unless the struct contains pointers and foo1
dereferences them.

--
Army1987 (Replace "NOSPAM" with "email")
"Never attribute to malice that which can be adequately explained
by stupidity." -- R. J. Hanlon (?)

Jul 15 '07 #16
CryptiqueGuy <SR**********@gmail.comwrites:
[...]
Many poorly written C books call "simulation of pass by reference
using pointers" as pass by reference which confuses a lot of Newbies.
I feel that Call by reference is said to be supported by a language IF
it syntactically and semantically supports that facility. C doesn't
have any syntactical and semantical facility for Call by Reference.
People find it difficult to understand that simple concept and call
"simulation of pass by reference using pointers" as REAL pass by
reference!
Right. C supports pass-by-reference in the same way that it supports
linked lists and many other things -- not as a built-in language
construct, but as something you can construct by programming.

--
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"
Jul 15 '07 #17
Roland Pibinger said:
On Sun, 15 Jul 2007 13:22:52 +0000, Richard Heathfield wrote:
>>C is a 100% call-by-value language. Function arguments are
expressions, not objects. These expressions are evaluated, and those
values are passed to the function.

This statement is not wrong but highly misleading.
Yes it's not wrong, but no it's not misleading either.
Esp. it doesn't
cover the distinction between the following:

void foo1 (stuct something sth);
void foo2 (stuct something* ptr);
But he didn't /ask/ about syntax errors.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jul 15 '07 #18
Richard <rg****@gmail.comwrote:
santosh <sa*********@gmail.comwrites:
Nehil wrote:
Does C follow call by value convention or call by reference?
i see that there is nothing like reference in C standard but it is
referenced.

still, what should be the answer for the above question?
C only supports call by value. However you can simulate call by
reference using pointers.
and arrays...
http://mces.blogspot.com/2005/08/pas...ence-in-c.html
No, For arrays there's a special rule. Arrays can't be passed
as a whole by value (this would require a copy of the whole
array each time the function is called and thus would be rather
slow) so when an array is found in a context where a value is
expected (e.g. when an array is used as a function argument
but also at the right hand side of an assignment). Instead when
found in such a context ("value context") it is converted auto-
matically to a pointer to the first element of the array, and
this value (i.e. the pointer to the first element of the array)
is what gets passed to the function.

This isn't really passing the array by reference since within
the function you have no information anymore about the 'array-
ness' of the pointer the function received, e.g. you don't
have any information about the size of the array (unless
passed to the function as an additional argument) and can't
distinguish it from a pointer that points to something else
than an array.

The article you cite is a rather useless example for what this
special rule can be (mis-)used - and the accompanying text is
simply false, there is no "automatic memory allocation" (what-
ever that is supposed to be) happening at all, there's just an
one-element array of structures, which is an automatic variable.

Regards, Jens
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://toerring.de
Jul 15 '07 #19
jt@toerring.de (Jens Thoms Toerring) writes:
[...]
No, For arrays there's a special rule. Arrays can't be passed
as a whole by value (this would require a copy of the whole
array each time the function is called and thus would be rather
slow) so when an array is found in a context where a value is
expected (e.g. when an array is used as a function argument
but also at the right hand side of an assignment). Instead when
found in such a context ("value context") it is converted auto-
matically to a pointer to the first element of the array, and
this value (i.e. the pointer to the first element of the array)
is what gets passed to the function.
[...]

Just a minor quibble: the idea that arrays are not passed as arguments
because they're too big is a shaky rationale. Structures (and unions)
can be passed as arguments (by value, of course), and structures can
be arbitrarily large. They can even contain arrays.

Arrays are "second-class" types because the language defines them that
way; it's a consequence of the way most operations on arrays are
defined in terms of pointer operations. (But arrays are not pointers,
and pointers are not arrays; see section 6 of the comp.lang.c FAQ.)

In the dim past, before the first C standard, structures were also
second-class types; you couldn't assign structure values, and you
couldn't pass them as function arguments. Operations on structures
were done with pointers. (This explains the odd way some of the
functions in the C standard library are declared; they were first
implemented before the rules changed.) It was found that the language
could be changed to allow structure assignment and argument passing
without breaking anything. The same is not true for arrays. Any
change allowing arrays to be treated as first-class types would break
existing code, so we're stuck with the historical baggage.

--
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"
Jul 15 '07 #20
Keith Thompson wrote:
[...] It was found that the language
could be changed to allow structure assignment and argument passing
without breaking anything. The same is not true for arrays. Any
change allowing arrays to be treated as first-class types would break
existing code, so we're stuck with the historical baggage.
Ritchie's "The Development of the C Language" (GIYF)
gives a pretty clear explanation of his motivation to
invent the "baggage."

(As for myself, I think highly of baggage. When the
airline loses it, I am not pleased.)

--
Eric Sosman
es*****@ieee-dot-org.invalid
Jul 15 '07 #21
Army1987 wrote:
On Sun, 15 Jul 2007 17:56:12 +0000, CryptiqueGuy wrote:
On Sun, 15 Jul 2007 13:22:52 +0000, Richard Heathfield wrote:
C is a 100% call-by-value language. Function arguments are
expressions,
not objects. These expressions are evaluated, and those values are
passed to the function.
This statement is not wrong but highly misleading. Esp. it doesn't
cover the distinction between the following:

void foo1 (stuct something sth);
void foo2 (stuct something* ptr);
^^^^^
What happened to 'r'? :-)
It's stuct in his keyboad.

--
Chis "puns ' us" Dollin

Hewlett-Packard Limited Cain Road, Bracknell, registered no:
registered office: Berks RG12 1HN 690597 England

Jul 16 '07 #22
Richard wrote:
Chris Dollin <eh@electrichedgehog.netwrites:
>Roland Pibinger wrote:
>>On Sun, 15 Jul 2007 13:22:52 +0000, Richard Heathfield wrote:
C is a 100% call-by-value language. Function arguments are expressions,
not objects. These expressions are evaluated, and those values are
passed to the function.

This statement is not wrong but highly misleading. Esp. it doesn't
cover the distinction between the following:

void foo1 (stuct something sth);
void foo2 (stuct something* ptr);

Two functions, distinguished by argument types only. So what?
Structs are passed by value, and pointers are passed by value.
What distinction are you referring to?

Because 99% of the time that C nOObs ask this question they are
enquiring how to pass a pointer to a large object as opposed to the
entire variable.
Oh, I /see/. Hmm. I'll try and bear that in mind the next time I
respond to that question -- the usual "what are you actually trying
to do?" should get mixed in.

--
Chris "impressed by the precision of '99%'" Dollin

Hewlett-Packard Limited Cain Road, Bracknell, registered no:
registered office: Berks RG12 1HN 690597 England

Jul 16 '07 #23

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by enjoying the view | last post: by
3 posts views Thread by jamihuq | last post: by
7 posts views Thread by Saeed Amrollahi | last post: by
15 posts views Thread by coolguyaroundyou | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.