C : Call by value or reference

E

Eric Sosman

Keith said:
[...] 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.)
 
R

Roland Pibinger

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.

The confusion stems from the fact that the word 'reference' is used in
different meanings (or at least in different contexts). C uses
pointers to reference objects: "A pointer type describes an object
whose value provides a reference to an entity of the referenced type.
A pointer type derived from the referenced type T is sometimes called
'pointer to T'." (C99 6.2.5:20). When a pointer is passed to a
function it is passed by value (the pointer value is copied to the
function, sometimes called 'pass by address'). The purpose of this
pointer pass by value is to keep a reference to the original object
(even though it's not pass by reference).
<offtopic>
Accordingly, not even Java uses pass by reference:
http://javadude.com/articles/passbyvalue.htm . OTOH, Java uses
references extensively and most people would agree that it employs
'reference semantics'.
C++ references allow pass by reference. Alas, a reference in C++ is
merely an "alias (an alternate name) for an object"
(http://www.parashift.com/c++-faq-lite/references.html#faq-8.1). So
C++ references don't reference anything, at least not in meaning used
for C pointers and Java references.
</offtopic>
 
N

Nehil

Roland said:
The confusion stems from the fact that the word 'reference' is used in
different meanings (or at least in different contexts). C uses
pointers to reference objects: "A pointer type describes an object
whose value provides a reference to an entity of the referenced type.
A pointer type derived from the referenced type T is sometimes called
'pointer to T'." (C99 6.2.5:20). When a pointer is passed to a
function it is passed by value (the pointer value is copied to the
function, sometimes called 'pass by address'). The purpose of this
pointer pass by value is to keep a reference to the original object
(even though it's not pass by reference).
<offtopic>
Accordingly, not even Java uses pass by reference:
http://javadude.com/articles/passbyvalue.htm . OTOH, Java uses
references extensively and most people would agree that it employs
'reference semantics'.
C++ references allow pass by reference. Alas, a reference in C++ is
merely an "alias (an alternate name) for an object"
(http://www.parashift.com/c++-faq-lite/references.html#faq-8.1). So
C++ references don't reference anything, at least not in meaning used
for C pointers and Java references.
</offtopic>

Thanks all for your answers.
 
C

Chris Dollin

Richard said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top