C : Call by value or reference

N

Nehil

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?
 
K

Kenny McCormack

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.
 
N

Nehil

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.
 
R

Richard Heathfield

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.
 
C

Chris Dollin

Nehil said:
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/.
 
S

santosh

Nehil said:
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.
 
R

Roland Pibinger

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);
 
R

Richard

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.
 
C

Chris Dollin

Roland said:
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?
 
K

Kenny McCormack

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.
 
R

Richard

Chris Dollin said:
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.
 
R

Richard

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.

--
 
C

CryptiqueGuy

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!
 
B

Barry Schwarz

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
 
A

Army1987

^^^^^
What happened to 'r'? :)
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.
 
K

Keith Thompson

CryptiqueGuy said:
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.
 
R

Richard Heathfield

Roland Pibinger said:
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.
 
J

Jens Thoms Toerring

and arrays...

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
 
K

Keith Thompson

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.
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top