473,786 Members | 2,426 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
22 4192
In article <6I************ *******@fe1.new s.blueyonder.co .uk>,
Chris Dollin <eh@electriched gehog.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@electriched gehog.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*****@xmissio n.xmission.com (Kenny McCormack) writes:
In article <6I************ *******@fe1.new s.blueyonder.co .uk>,
Chris Dollin <eh@electriched gehog.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.c omwrote:
rpbg...@yahoo.c om (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...@xmissio n.xmission.com (Kenny McCormack)
wrote:
>In article <1184504183.456 641.240...@g4g2 000hsf.googlegr oups.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**********@g mail.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_Keit h) 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.c omwrote:
santosh <sa*********@gm ail.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_Keit h) 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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
1478
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 server stub. The client stub has the same functions defined, but they just connect to the server and ask it to call the desired functions. The server stub is a server listening for incoming requests and dispatching them to the appropriate functions...
3
3209
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
2456
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 have 3 vector variables in Main(). I want the read function to read the values into the vector using the address I send of the vectors.. Sample code: //x.cpp void read(vector <int> a,vector <int> b,vector < vector <int> > c)
10
16675
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
2295
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
4316
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
3523
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 performance with call- by-reference vs. call-by-value. Indeed, the perforamnce is absolutely unconsiderable: #include <vector>
15
1736
by: coolguyaroundyou | last post by:
Does C have Call-By-Reference for a function?? I think it doesn't. Am I right?
0
10363
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10164
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10110
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9961
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7512
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6745
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5534
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4066
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3669
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.