Answers for "difference between pointer and reference in c++"

C++
5

c pointers vs references

Pointers: A pointer is a variable that holds memory address of another variable. A pointer needs to be dereferenced with * operator to access the memory location it points to.

References : A reference variable is an alias, that is, another name for an already existing variable. A reference, like a pointer, is also implemented by storing the address of an object.
A reference can be thought of as a constant pointer (not to be confused with a pointer to a constant value!) with automatic indirection, i.e the compiler will apply the * operator for you.
*******************************************
*******************************************
  
A pointer can be re-assigned:

int x = 5;
int y = 6;
int *p;
p = &x;
p = &y;
*p = 10;
assert(x == 5);
assert(y == 10);
-----------------------------------------
 A reference cannot, and must be assigned at initialization:

int x = 5;
int y = 6;
int &r = x;
-------------------------------------------
A pointer has its own memory address and size on the stack (4 bytes on x86), whereas a reference shares the same memory address (with the original variable) but also takes up some space on the stack. Since a reference has the same address as the original variable itself, it is safe to think of a reference as another name for the same variable. Note: What a pointer points to can be on the stack or heap. Ditto a reference. My claim in this statement is not that a pointer must point to the stack. A pointer is just a variable that holds a memory address. This variable is on the stack. Since a reference has its own space on the stack, and since the address is the same as the variable it references. More on stack vs heap. This implies that there is a real address of a reference that the compiler will not tell you.

int x = 0;
int &r = x;
int *p = &x;
int *p2 = &r;
assert(p == p2);
-------------------------------------------
You can have pointers to pointers to pointers offering extra levels of indirection. Whereas references only offer one level of indirection.

int x = 0;
int y = 0;
int *p = &x;
int *q = &y;
int **pp = &p;
pp = &q;//*pp = q
**pp = 4;
assert(y == 4);
assert(x == 0);
-------------------------------------------
A pointer can be assigned nullptr directly, whereas reference cannot. If you try hard enough, and you know how, you can make the address of a reference nullptr. Likewise, if you try hard enough, you can have a reference to a pointer, and then that reference can contain nullptr.

int *p = nullptr;
int &r = nullptr; <--- compiling error
int &r = *p;  <--- likely no compiling error, especially if the nullptr is hidden behind a function call, yet it refers to a non-existent int at address 0
Pointers can iterate over an array; you can use ++ to go to the next item that a pointer is pointing to, and + 4 to go to the 5th element. This is no matter what size the object is that the pointer points to.

-------------------------------------------
A pointer needs to be dereferenced with * to access the memory location it points to, whereas a reference can be used directly. A pointer to a class/struct uses -> to access it's members whereas a reference uses a ..
-------------------------------------------
References cannot be stuffed into an array, whereas pointers can be!
-------------------------------------------

Const references can be bound to temporaries. Pointers cannot (not without some indirection):

const int &x = int(12); //legal C++
int *y = &int(12); //illegal to dereference a temporary.
This makes const& safer for use in argument lists and so forth.
Posted by: Guest on September-12-2020
3

difference between pointer and reference

//Passing by Pointer://

// C++ program to swap two numbers using 
// pass by pointer. 
#include <iostream> 
using namespace std; 
  
void swap(int* x, int* y) 
{ 
    int z = *x; 
    *x = *y; 
    *y = z; 
} 
  
int main() 
{ 
    int a = 45, b = 35; 
    cout << "Before Swap\n"; 
    cout << "a = " << a << " b = " << b << "\n"; 
  
    swap(&a, &b); 
  
    cout << "After Swap with pass by pointer\n"; 
    cout << "a = " << a << " b = " << b << "\n"; 
} 
o/p:
Before Swap
a = 45 b = 35
After Swap with pass by pointer
a = 35 b = 45

//Passing by Reference://

// C++ program to swap two numbers using 
// pass by reference. 
  
#include <iostream> 
using namespace std; 
void swap(int& x, int& y) 
{ 
    int z = x; 
    x = y; 
    y = z; 
} 
  
int main() 
{ 
    int a = 45, b = 35; 
    cout << "Before Swap\n"; 
    cout << "a = " << a << " b = " << b << "\n"; 
  
    swap(a, b); 
  
    cout << "After Swap with pass by reference\n"; 
    cout << "a = " << a << " b = " << b << "\n"; 
} 
o/p:
Before Swap
a = 45 b = 35
After Swap with pass by reference
a = 35 b = 45

//Difference in Reference variable and pointer variable//

References are generally implemented using pointers. A reference is same object, just with a different name and reference must refer to an object. Since references can’t be NULL, they are safer to use.

A pointer can be re-assigned while reference cannot, and must be assigned at initialization only.
Pointer can be assigned NULL directly, whereas reference cannot.
Pointers can iterate over an array, we can use ++ to go to the next item that a pointer is pointing to.
A pointer is a variable that holds a memory address. A reference has the same memory address as the item it references.
A pointer to a class/struct uses ‘->'(arrow operator) to access it’s members whereas a reference uses a ‘.'(dot operator)
A pointer needs to be dereferenced with * to access the memory location it points to, whereas a reference can be used directly.
Posted by: Guest on October-11-2020
1

pointers vs references in c++

#include<iostream>

/*
Pointers: *ptr, point to the memory location of a variable
int a = 10
int *ptr = &a //points to the location in memory (0x80ea or whatever) 
instead of the value

in order for pointers to work, the variable it's pointing to needs to 
be de-referenced using &.(If confused, remember that the variable, int a, 
is itself a reference to the location of the value you set it to).

A reference variable: &ref, points to another variable.

int b = 20;
int &ref = b // points to the value of b, which is 20.

run this if confused:
*/

    int a = 10;
    int *ptr = &a;
    std::cout << "int a value: " << a << std::endl;
    std::cout << "int ptr value: " << ptr << std::endl;

    int b = 20;
    int& ref = b;
    std::cout << "int b value: " << b << std::endl;
    std::cout << "int ref value: " << ref << std::endl;

    ref = a;
    std::cout << "int ref after setting it equal to a: " << ref << std::endl;
    ref = *ptr;
    std::cout << "int ref after setting it equal to *ptr: " << ref << std::endl;
    ptr = &ref;
    std::cout << "ptr after setting it equal to &ref: " << ptr << std::endl; 
    ptr = &b;
    std::cout << "ptr after setting it equal to &b: " << ptr << std::endl;

/*
Reference variables CANNOT be set to a pointer variable; In the case above, you 
see we can't just put ref = ptr; ptr HAS to be dereferenced with a *, which in 
turn will give us the value of a, or 10. (dereference pointers with *)

Same goes for pointer variables being set to a reference; you have to dereference 
the reference value (ptr = &b instead of ptr = b;). In the block above, when we 
set ptr = &ref, the ref variable is dereferenced showing us a memory location. 
When ptr=&b is called and we see the output, we noticed it is the same as the previous 
output.
*/
Posted by: Guest on September-10-2021
1

difference between pointer and reference in c++

Pointers: 
A pointer is a variable that holds memory address of another variable. 
A pointer needs to be dereferenced with * operator to access the 
memory location it points to. 

References :
 A reference variable is an alias, that is, 
another name for an already existing variable.
 A reference, like a pointer, is also implemented 
by storing the address of an object.
Posted by: Guest on May-14-2021

Code answers related to "difference between pointer and reference in c++"

Browse Popular Code Answers by Language