Passing arguments to functions using pointers and reference

Hello, cpp forum members and community. I am a new member and this will be my first post, of many, hopefully (and not all just about asking for advice, like this one).

Well to start with stating my problem: I am reading a book on C++, i am a beginner to the language and the world of programming entirely, but have been studying the book for about two months. So i feel i have a basic understanding of the syntax and how to use simple functions to make very basic programs.

One example in the book's chapter on functions and passing arguments to functions had only covered passing arguments by reference as opposed to passing by value and this is what i had been doing for just about every exercise that followed.

Example:
1
2
3
4
5
6
7
8
9
10
11
int someFn(int &variable)
{
  variable += 1;
  return variable;
}
int main()
{
  int variable = 0;
  someFn(variable);
  /* variable is now = 1 */
}


Basic things like that. I believe I understand the concept of using the reference pointer in this situation. Today I looked in the chapter regarding classes and the author decides to go back into passing variables from main into functions using pointers and then states:
"Passing by reference is just another way of passing the address of the object.
C++ keeps track of the address of a reference, whereas you manipulate the
address in a pointer."


Here is the example code (not word for word, assuming that the book's source code is copyright):
1
2
3
4
5
6
7
8
9
10
int someFn(int* variable)
{
  *variable = 10;
}
int main()
{
  int variable = 1;
  someFn(&variable);
  /* 'Variable' is now = 10 */
}


I've used both of these techniques a few times and in a few different ways and still don't understand what the author was implying when he said "C++ keeps track of the address of a reference and manipulates the variable by using a pointer". To me they seem to perform the same operation. It seems a bit like: "You say tomato, I say tommatto." and I just wanted to be clear on this. I have a faint feeling I might just be over thinking it and that I am right in assuming he is saying its just another way to do the same thing.

That being said, I'd like to go ahead and cover all my bases on this so it doesn't come back to bite me later.

So, could someone please give me a few examples of where favoring one over the other might come in handy (if at all) and if it would, why it would as well? :)
closed account (zb0S216C)
The differences between pointers and references are relatively small.

Differences:

- Pointers:
- Requires dereferencing to modify the value it's pointing to.
- Accepts null.

- References:
- Doesn't require dereferencing to modify the value it's pointing to.
- Doesn't accept null.

As you can see, there's only a few noticeable differences between the two. Both pointer and reference prevent copies of the passed value being made.

Locke wrote:
please give me a few examples of where favoring one over the other might come in handy

Sure.

Example #1: This example defines a function that takes a single argument. The given argument is multiplied by itself. The result of the compound expression is then returned to the calling function.
1
2
3
4
5
6
7
8
9
10
11
12
13
int Function( int * );

int main( )
{
    return 0;
}

int Function( int *Param )
{
    return( Param * Param );

    // OK, what if I pass NULL (nullptr if you're compiler supports C++0x)?
}


Use references when you don't wan't null to be given. Passing null as an argument when a value is expected can be bad since you're not pointing to a valid address.

Example #2: This example is the same as the one above, except that the pointer has been switched with a reference. With a reference, null isn't an option, thus you always refer to a valid address.
1
2
3
4
5
6
7
8
9
10
11
int Function( int & );

int main( )
{
    return 0;
}

int Function( int &Param )
{
    return( Param * Param );
}


Wazzak
Last edited on
IMO, they are overexplaining what the compiler *might* be doing with the reference. Basically, a reference is like pre-dereferenced pointer, and a pointer isn't.

It sounds like what they are trying to get at is that usually references are implemented as pointers internally, but that isn't something you should know (or care) about.
Referencia:

A referencián keresztül feltöltő függvény:

void ott_helyben(adattipus&) prototípus
void ott_helyben(adattipus& adattípusu_nev) definíció
{
adattipusu_nev="Ott kapsz értéket!"
return;
}

a hívó függvény:

adattípus itt_toltsd_fel;
ott_helyben(itt_toltsd_fel)

Az adattípus lehet pl. nagy méretű még csak definiált vector.
Így nem kell a tömb feltöltését a verem közbeiktatásával végezni.
Thank you, Framework, for taking the time to explain and for giving me those helpful examples as well, I think I understand better now.

Firedraco, thank you for your explanation as well. I believe this is what the author was trying to say to, but I just wanted to follow up and see if there was any difference in using either one in a specific situation.

Thank you too Esetleg, but I'll be honest, I'm not quite sure what you were trying to say. I put what you said in a translator and got back that you were trying to explain the concept to me (only in Hungarian instead of English). The translator couldn't really pick up all the words so I'm not too sure what you said, but thank you for the reply all the same.

And thank you to anyone else who decides to post further on this topic here as well. :)

~~Marked as solved.
Last edited on
Topic archived. No new replies allowed.