ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I will explain my question through an example:
void swap(int *a, int *b)
void swap(int &a, int &b)
Question 1: are both of these techniques (the first technique using pointers and the second using addresses) called "call by reference"?
Question 2: both of these have the same result when they are used; the first one uses addresses as arguments and the second one pointers.
Which is their difference? Which one is faster? Which one uses more memory space? When should i use the first technique and when should i use the second one?
C has no such thing as "pass by reference". You can only pass values to functions in C. void swap(int &a, int &b); is not valid C code. In C you would do something like this:
Code:
void swap(int *a, int *b)
{
int temp;
temp = *a;
*a = *b;
*b = temp;
}
int main(void)
{
int a = 5, b = 7;
printf("a = %d, b = %d\n", a, b);
swap(&a, &b);
printf("a = %d, b = %d\n", a, b);
return 0;
}
swap(&a, &b); may look like you're passing by reference, but you're really passing a value; the address of the variable.
C++ allows passing by reference, using the syntax you've described. Note: C++, NOT C. Don't confuse the two. Enough people do as it is...
itsme86 demonstrated the C way. C++ allows you to do it like this:
Code:
void swap(int& a, int& b)
{
int temp;
temp = a;
a = b;
b = temp;
}
int main() //no void in the declaration, for it is Bad and Vile.
{
int a = 5, b = 7;
printf("a = %d, b = %d\n", a, b);
swap(a, b);
printf("a = %d, b = %d\n", a, b);
//no return code necessary
}
Last edited by SoulSkorpion; 10-20-2004 at 06:34 AM.
Passing using * and & do the same thing internally; they pass a pointer. The use of & is a semantical convenience allowed by the compiler so that you don't have to do *a = or a-> to use the argument; it makes the pointer appear like an ordinary object. Another reason to use a reference is so you don't have to check for a bad pointer in the function: even though the reference operation actually passes a pointer, it must be a valid object (unless you dereference a bad pointer as the argument.) This means that NULL cannot be passed as would be allowed by a pointer argument. The bad thing is temp objects can't be used; if your function asks for int&, you cannot give it a char argument; even though it converts if by value, the references cannot be converted. Therefore; do not use for POD unless you are changing the value.
Pass by reference merely means that the argument is not copied. A * argument is actually a pointer, which is a value, therefore that is a pointer argument being passed by value. You can, however pass a pointer by reference with *&, thereby allowing you to change the pointer value, to say a new[] array. You can have multiple pointer types e.g. int**, but you can only use one reference per type, and cannot take a pointer to a reference; int*& OK int**& OK int&* NOT OK int&& NOT OK.
ta0kira
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.