Last active
July 29, 2019 09:07
-
-
Save sprytnyk/d07fd8f9002cd0c18e8932647327e04c to your computer and use it in GitHub Desktop.
Differences between pointers & references from C++ perspective.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
int main() { | |
/* | |
* References are often confused with pointers but three major | |
* differences between references and pointers are: | |
* - You cannot have NULL references. You must always be able to assume | |
* that a reference is connected to a legitimate piece of storage. | |
* - Once a reference is initialized to an object, it cannot be | |
* changed to refer to another object. Pointers can be pointed to | |
* another object at any time. | |
* - A reference must be initialized when it is created. Pointers can | |
* be initialized at any time. | |
* */ | |
int x(0); | |
int y(1); | |
// A reference refers to the same memory address, so it shares | |
// the same memory address, but not holds like a pointer and not | |
// creates a new one. | |
int &alpha = x; | |
// A pointer is a variable that holds a memory address where a value lives. | |
int *beta = &y; | |
// "Regular" variables (not pointers), they have memory address too, but not holds them. | |
int default_behavior = x; | |
cout << "Address of x " << &alpha << " is the same like of alpha " << &x << endl; | |
cout << "Address of y " << &y << " isn't the same like of beta " << &beta << endl; | |
cout << endl; | |
cout << "Value of x is " << x << " and value of alpha is " << alpha << endl; | |
cout << "Value of y is " << y << " and value of beta is " << beta << endl; | |
cout << "#########################################################################" << endl; | |
cout << "Default behavior address " << &default_behavior << " and value " << default_behavior << endl; | |
cout << "#########################################################################" << endl; | |
// Assignment of a new value to the alpha variable sets this value as | |
// well to x. As far as they share the same memory space, we can access | |
// this value without any syntax sugar. | |
alpha = 10; // reference | |
// Assignment of a new value to the beta pointer sets this value only | |
// to y. As far as beta holds the only memory address to get/assign a | |
// value we need to use syntax sugar *. | |
*beta = 20; // pointer | |
cout << "Values and memory addresses after reassignment." << endl; | |
cout << endl; | |
cout << "Address of x " << &alpha << " is the same like of alpha " << &x << endl; | |
cout << "Address of y " << &y << " isn't the same like of beta " << &beta << endl; | |
cout << endl; | |
cout << "Value of x is " << x << " and value of alpha is " << alpha << endl; | |
cout << "Value of y is " << y << " and value of beta is " << beta << endl; | |
cout << endl; | |
// Eventually we see changes only in ints values but not in memory addresses. | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment