User Name:


Forgot your password?
Vote for Us!
tintin++ ogg sound player script for linux
Author: Robert Smith
Submitted by: Vladaar
6Dragons ogg Soundpack
Author: Vladaar
Submitted by: Vladaar
6Dragons 4.4
Author: Vladaar
Submitted by: Vladaar
LoP 1.46
Author: Remcon
Submitted by: Remcon
LOP 1.45
Author: Remcon
Submitted by: Remcon
Users Online
CommonCrawl, Bing, Majestic-12, Yandex

Members: 0
Guests: 3
Newest Member
Today's Birthdays
There are no member birthdays today.
Related Links
» SmaugMuds.org » General » Coding » Call by reference in C
Forum Rules | Mark all | Recent Posts

Call by reference in C
< Newer Topic :: Older Topic >

Pages:<< prev 1 next >>
Post is unread #1 Jun 26, 2009, 10:54 am   Last edited Jun 26, 2009, 10:57 am by Tonitrus
Go to the top of the page
Go to the bottom of the page

JoinedJun 24, 2009


Last night I encountered a problem with a function I wrote to add an affect to a linked list. It took 3 parameters, each AFFECT_DATA pointers. (AFFECT_DATA *af, AFFECT_DATA *first, AFFECT_DATA *last) This function was just a wrapper for the LINK(), although I also tried writing my own linking function, but neither worked. Anyway, I fixed the issue by changing first and last to AFFECT_DATA **first, AFFECT_DATA **last, and fed *first and *last to LINK().

I have a theory on why this is necessary that I have been entertaining, and since I already average about a post every 2 hours since I joined, I figured I'd run it by you fine people so you can correct me if I'm wrong before I get attached to the idea.

(I have no formal training, etc, so I learn things by watching what happens when things break, or, failing that, I just make stuff up)

Anyway, my theory is that it is related to this "call by value" and "call by reference" crap, which I always confuse, but I looked up recently, and wikipedia, etc says that C uses call by value by default, but you can make it use call by reference by passing pointers.

However, this "linking" incident has led me to believe that C always calls by value, and when you pass a pointer, you pass the value (i.e., address) of the pointer, which is fine for most things, unless you try to set the pointer itself, in which case you have to take another level of indirection in order to get the desired results.

Post is unread #2 Jun 26, 2009, 11:04 am
Go to the top of the page
Go to the bottom of the page

David Haley
JoinedJan 29, 2007

C always calls by value. "Calling by reference" in C means that you pass the value of a reference, or put perhaps less confusingly, you pass a pointer to a value. In other words, even though you are technically calling something by value, the effect in practice of passing a pointer is calling by reference where you are referencing the thing pointed to.

C++, evil as it may be to you, introduces very easy call-by-reference syntax. :smile:
Post is unread #3 Jun 26, 2009, 11:12 am
Go to the top of the page
Go to the bottom of the page

JoinedMar 8, 2005

That is correct. C always passes by value, and both the caller and the function being called have to know how many dereferences to apply.

So, if you want to modify a pointer, you'd pass its address via func(&p), and then func() has to know that to modify the pointer, it uses *p, but to modify the value it points at it uses **p, or in the case of structures (*p)->foo (which is the same as **p.foo).

Note that passing a pointer isn't really 100% the same as call-by-reference. It will do the same job, but some languages enforce things more brutaly. Passing by reference in something like ADA does NOT result in a local parameter variable being created and managed, you actually work directly with the storage address. In C, you get a copy of the address which is stored in a local (stack) variable. The difference is not worth noting unless you're working with embedded systems where bytes and cycle counts matter (ADA was used by the DOD for things like missile guidance systems -- they now use C++).
Pages:<< prev 1 next >>