473,372 Members | 846 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,372 software developers and data experts.

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

Similar topics

6
by: enjoying the view | last post by:
I am working on a school project, trying to build a simple RPC stub generator. The idea is that the generator takes a normal python file with functions defined in it and produces a client and...
3
by: jamihuq | last post by:
Hello all, I have a question. Does the concept of call by reference exist in C like it does in C++? And if so, can someone give me an example. Thanks Jami
1
nabh4u
by: nabh4u | last post by:
Hi, I have a problem referencing to Vectors using pointers i.e. I am not able to use "call by reference" on vector variables. I have a "read()" function in "x.cpp" and "main()" in "y.cpp". I...
10
by: ravi | last post by:
Hi, i am a c++ programmer, now i want to learn programming in c also. so can anybody explain me the difference b/w call by reference and call by pointer (with example if possible).
10
by: psbasha | last post by:
Hi, Is Python supports Call by reference and Call by address in function or methods,without using C/C++ modules?. Thanks in advacne PSB
3
Digital Don
by: Digital Don | last post by:
I have a problem sending the Vectors as Referencial parameters..i.e. I am having a struct vector struct board { int r; int c; };
7
by: Saeed Amrollahi | last post by:
Dear All Hi I have learned when an object is big, I should pass the object using a reference or pointer to it rather than calling by value. In the following code, I don't gain cosiderable...
15
by: coolguyaroundyou | last post by:
Does C have Call-By-Reference for a function?? I think it doesn't. Am I right?
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.