Click here to Skip to main content
15,557,873 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I was learning about references in c++ and I realised that pointers could do the same thing:

with reference:
C++
int hello(int& hello)
{
    return hello;
}
int main()
{
    cout << "enter a num: ";
    int num;
    cin >> num;
    cout << num << endl;
    hello(num);
    cout << endl;
    cout << *num;
}
Outputs:
12

12

with pointer:
C++
int hello(int* hello)
{
    return *hello;
}
int main()
{
    cout << "enter a num: ";
    int *num = new int;
    cin >> *num;
    cout << *num << endl;
    hello(num);
    cout << endl;
    cout << *num;
}
Outputs:
12

12

My Question:
Since they both do the same thing(alias a variable), why do we use references over a pointer?

What I have tried:

i have tried looking it up, but cant find anything
Posted
Updated 7-Jan-23 10:02am
v2
Comments
Joe Woodbury 5-Jul-21 16:21pm    
How does cout << *num; even compile?

A reference parameter tells the user that nullptr is invalid, whereas a pointer parameter might allow it.

A reference also allows you to write parameter.whatever, whereas the pointer requires an extra character: parameter->whatever. That's a big deal to some people! :)

But even though a reference says "I don't accept nullptr", it can still get one, so bulletproof code needs to include if(&ref != nullptr).

EDIT: Your code should include cout << hello(num) to better demonstrate your point. As it stands, hello(num) isn't doing anything because main ignores what it returns.
 
Share this answer
 
v4
A main difference is that pointers point normally to on the heap allocated memory (like structs or objects) and references mostly refer to variables from the stack. It becomes more important when bigger data chunks like object arrays, multimedia data or databases are processed.

Read this Stack vs Heap Memory Allocation to better understand this advanced topic. Some even more detailled video about this topic.
 
Share this answer
 
Mechanically, they are the same. There are two main usage difference though:

1) They cannot be NULL and they have to be passed in the code: any mistake is easily discovered at compile time.
2) You won't need to dereference them when assigning them values. That helps readability.

Also:
* You cannot inadvertantly modify the address they're pointing to;
* You don't have to / can't cast them, which helps being type safe.
 
Share this answer
 
Comments
Greg Utas 5-Jul-21 9:03am    
A reference can most assuredly be nullptr. The invoker of a function can dereference a pointer and pass it to a reference argument: f(*p). If p is nullptr and the invoker never dereferenced p to access one of its fields, the crash occurs in f.
Pointers are more powerful than references. Unfortunately pointer usage is more prone to errors. So, the rule of thumb is: use a references everywhere you can. Use pointers where you cannot use references.
 
Share this answer
 
References are easier to use than pointer variables. The main difference that seperates them is that reference variables are used to point at a single variable and can also make changes to it whereas, pointer variables can be used to point at multiple variables(not at the same time). For further understanding, I suggest that you check relationship between pointers and arrays. Hope that answers your question. (I still couldn't figure out in which perspective you were asking in,but if there is still any confusion please try being more specific)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900