1Pointers: 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.
2
3References : 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.
4A 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.
5*******************************************
6*******************************************
7
8A pointer can be re-assigned:
9
10int x = 5;
11int y = 6;
12int *p;
13p = &x;
14p = &y;
15*p = 10;
16assert(x == 5);
17assert(y == 10);
18-----------------------------------------
19 A reference cannot, and must be assigned at initialization:
20
21int x = 5;
22int y = 6;
23int &r = x;
24-------------------------------------------
25A 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.
26
27int x = 0;
28int &r = x;
29int *p = &x;
30int *p2 = &r;
31assert(p == p2);
32-------------------------------------------
33You can have pointers to pointers to pointers offering extra levels of indirection. Whereas references only offer one level of indirection.
34
35int x = 0;
36int y = 0;
37int *p = &x;
38int *q = &y;
39int **pp = &p;
40pp = &q;//*pp = q
41**pp = 4;
42assert(y == 4);
43assert(x == 0);
44-------------------------------------------
45A 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.
46
47int *p = nullptr;
48int &r = nullptr; <--- compiling error
49int &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
50Pointers 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.
51
52-------------------------------------------
53A 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 ..
54-------------------------------------------
55References cannot be stuffed into an array, whereas pointers can be!
56-------------------------------------------
57
58Const references can be bound to temporaries. Pointers cannot (not without some indirection):
59
60const int &x = int(12); //legal C++
61int *y = &int(12); //illegal to dereference a temporary.
62This makes const& safer for use in argument lists and so forth.
1#include<iostream>
2
3/*
4Pointers: *ptr, point to the memory location of a variable
5int a = 10
6int *ptr = &a //points to the location in memory (0x80ea or whatever)
7instead of the value
8
9in order for pointers to work, the variable it's pointing to needs to
10be de-referenced using &.(If confused, remember that the variable, int a,
11is itself a reference to the location of the value you set it to).
12
13A reference variable: &ref, points to another variable.
14
15int b = 20;
16int &ref = b // points to the value of b, which is 20.
17
18run this if confused:
19*/
20
21 int a = 10;
22 int *ptr = &a;
23 std::cout << "int a value: " << a << std::endl;
24 std::cout << "int ptr value: " << ptr << std::endl;
25
26 int b = 20;
27 int& ref = b;
28 std::cout << "int b value: " << b << std::endl;
29 std::cout << "int ref value: " << ref << std::endl;
30
31 ref = a;
32 std::cout << "int ref after setting it equal to a: " << ref << std::endl;
33 ref = *ptr;
34 std::cout << "int ref after setting it equal to *ptr: " << ref << std::endl;
35 ptr = &ref;
36 std::cout << "ptr after setting it equal to &ref: " << ptr << std::endl;
37 ptr = &b;
38 std::cout << "ptr after setting it equal to &b: " << ptr << std::endl;
39
40/*
41Reference variables CANNOT be set to a pointer variable; In the case above, you
42see we can't just put ref = ptr; ptr HAS to be dereferenced with a *, which in
43turn will give us the value of a, or 10. (dereference pointers with *)
44
45Same goes for pointer variables being set to a reference; you have to dereference
46the reference value (ptr = &b instead of ptr = b;). In the block above, when we
47set ptr = &ref, the ref variable is dereferenced showing us a memory location.
48When ptr=&b is called and we see the output, we noticed it is the same as the previous
49output.
50*/
1//Passing by Pointer://
2
3// C++ program to swap two numbers using
4// pass by pointer.
5#include <iostream>
6using namespace std;
7
8void swap(int* x, int* y)
9{
10 int z = *x;
11 *x = *y;
12 *y = z;
13}
14
15int main()
16{
17 int a = 45, b = 35;
18 cout << "Before Swap\n";
19 cout << "a = " << a << " b = " << b << "\n";
20
21 swap(&a, &b);
22
23 cout << "After Swap with pass by pointer\n";
24 cout << "a = " << a << " b = " << b << "\n";
25}
26o/p:
27Before Swap
28a = 45 b = 35
29After Swap with pass by pointer
30a = 35 b = 45
31
32//Passing by Reference://
33
34// C++ program to swap two numbers using
35// pass by reference.
36
37#include <iostream>
38using namespace std;
39void swap(int& x, int& y)
40{
41 int z = x;
42 x = y;
43 y = z;
44}
45
46int main()
47{
48 int a = 45, b = 35;
49 cout << "Before Swap\n";
50 cout << "a = " << a << " b = " << b << "\n";
51
52 swap(a, b);
53
54 cout << "After Swap with pass by reference\n";
55 cout << "a = " << a << " b = " << b << "\n";
56}
57o/p:
58Before Swap
59a = 45 b = 35
60After Swap with pass by reference
61a = 35 b = 45
62
63//Difference in Reference variable and pointer variable//
64
65References 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.
66
67A pointer can be re-assigned while reference cannot, and must be assigned at initialization only.
68Pointer can be assigned NULL directly, whereas reference cannot.
69Pointers can iterate over an array, we can use ++ to go to the next item that a pointer is pointing to.
70A pointer is a variable that holds a memory address. A reference has the same memory address as the item it references.
71A pointer to a class/struct uses ‘->'(arrow operator) to access it’s members whereas a reference uses a ‘.'(dot operator)
72A pointer needs to be dereferenced with * to access the memory location it points to, whereas a reference can be used directly.
1Pointers:
2A pointer is a variable that holds memory address of another variable.
3A pointer needs to be dereferenced with * operator to access the
4memory location it points to.
5
6References :
7 A reference variable is an alias, that is,
8another name for an already existing variable.
9 A reference, like a pointer, is also implemented
10by storing the address of an object.