The value of a pointer is an address. The expression "&a" evaluates to a value which is an address. So, in your example: at runtime the values of "&a" and "&b" are first evaluated before being passed to the function. The address values are then copied into the pointers "x" and "y" in the stack frame allocated for the "swap" function. The pass by semantics have to do with the underlying behavior. Not with the fact that C provides pointers that allow us to do indirect addressing.
pass by reference in c----help banfa
Collapse
X
-
Originally posted by jericostope talking these craps.
The answer to your question is that C only passes parameters by value. However the value of those parameters passed may in fact be the memory location of a variable external to a function allowing the function to alter the value of that external variable.
The mistake I made centres around this statement by tyreld
"Passing semantics only refer to the parameter being passed."
and my tendancy to think at machine level.
I am (more or less) correct that at machine level there is no such thing as pass by reference, there is only pass by value although that value may be the memory location of another variable.
"pass by reference" and "pass by value" are only applicable at the language level because even if a language "passes by reference" it is likely that once this is translated to machine level this results in a pass by value of a memory location.
As such C passes by value, this should not be a surprise because C is and always has been only 1 step away from assembler (the machine level). In fact I believe C was designed to have easy and in many cases direct translation from C syntax to assembler/machine code.
Higher level languages with more abstraction from the machine (VB, Java) may or may not pass by reference (VB does by default I believe). This is part of the language semantics, i.e. how the language works as viewed by the user, not how it works on the machine.
Whatever the case it is imperitive that you know and understand the passing semantics for the language you are using.Comment
-
Originally posted by jericoI was only referring to the posts of vermarajeev.Not others.I hav posted here to learn something , not for fun.Anyway thanks for reply.
jerico
What do you mean talking that way?????
Who is doing fun out here????Comment
-
Originally posted by jericoI was only referring to the posts of vermarajeev.Not others.I hav posted here to learn something , not for fun.Anyway thanks for reply.
jericoOriginally posted by vermarajeevHey man!!
What do you mean talking that way?????
Who is doing fun out here????
May be you did start this thread jerico but it you look at the post vermarajeev made that you have complained at it ended with a big question .
vermarajeev is clearly not entirely clear on the subject and, given that this is a public forum, has every right to join in the conversation and ask what ever question or give whatever example they feel they need to in order to clarify the situation as long as it is on topic for the thread.
If you think you understand something then attempting to explain it to someone else is a fine way of concreating it in your brain and if you know that there are other knowledgible people listening also a good way of making sure you have things correct since if you are wrong you will probably be corrected.
Have you not heard the phase "see one, do one, teach one", as borrowed from the medical profession.Comment
-
Originally posted by BanfaStop bickering or I'll send you both to bed without supper :)
May be you did start this thread jerico but it you look at the post vermarajeev made that you have complained at it ended with a big question .
vermarajeev is clearly not entirely clear on the subject and, given that this is a public forum, has every right to join in the conversation and ask what ever question or give whatever example they feel they need to in order to clarify the situation as long as it is on topic for the thread.
If you think you understand something then attempting to explain it to someone else is a fine way of concreating it in your brain and if you know that there are other knowledgible people listening also a good way of making sure you have things correct since if you are wrong you will probably be corrected.
Have you not heard the phase "see one, do one, teach one", as borrowed from the medical profession.
thankx for your suggestion,
Jerico, sorry, if I have discommode you
ThankxComment
Comment