+ 11

What are the differences between references and pointers?

pointers and reference

2nd Dec 2016, 7:25 PM
sadiya zainab
sadiya zainab - avatar
5 ответов
+ 25
1. A reference is an alias to an existing variable. A pointer is a variable that holds the address of another variable. 2. It is mandatory to initialise a reference which is not true with a pointer. 3. A reference once initialised can not be reassigned. Where as you can re assign a pointer. 4. A pointer can be assigned to null where as a reference can not be. 5. We can have a pointer to another pointer but we can not have a reference to another reference. 6. A pointer has an address where as a reference has the same address as its alias has. 7. There is pointer arithmetic but no reference arithmetic. 8. We can not have an array of reference. Update : Adding two more points. 9. You can have a reference to a literal if the declaration specifier sequence contains a const qualifier where as under no circumstances except when the literal is of type 'const char *' you are allowed to have a pointer to a literal. That is : const int &ref = 5; // fine const int *ptr = &5 // illegal const char *ptr = "Hello World!"; // fine 10. On the lines similar to point 9, we can have a mismatched type reference if the declaration specifier sequence of a reference variable contains a const qualifier. This is not true with a pointer however. That is: float float_var = 12.34F; const int &ref = float_var; // fine anf compiles const int *ptr = &float_var; // compilation error
6th Dec 2016, 9:12 AM
Sourabh Khandelwal
Sourabh Khandelwal - avatar
+ 4
pointers point to the memory address the variable is stored to and allows you to handle variables of unknown sizes when they are referred back to (using the heap). references are the value of the variable held at the address in the memory.
2nd Dec 2016, 8:13 PM
Alex Lewis
Alex Lewis - avatar
0
These answers are very helpful for me :) Now i can understand, thanks people :)
6th Dec 2016, 7:10 PM
Cenk Camkıran
Cenk Camkıran - avatar
- 1
refrences not To pointing To the address.
19th Dec 2016, 9:24 AM
Seyedjalal Hesami
Seyedjalal Hesami - avatar