C++ Pointers
-
What are Pointers
Pointer is variable that stores memory adress of another variable
-
Creating a Pointer
To create pointer you use type and
*
operator// creating null pointer int* ptr = 0;
You can get memory adress of variable by using
&
operatorint a = 5; int* ptr = &a; // ptr holds adress of 'a'
You get a value of variable that pointer is pointing at by using
*
operatorint a = 5; int* ptr = &a; *ptr = 10; // now a is 10
-
What are References
A reference is an alias, or an alternate name to an existing variable. You can then manipulate it to change value of referencing variable.
Reference variable must be set when creating reference. -
Creating references
You can create reference using
&
operatorint a = 5; int& ref = a; // it stores a reference to a ref = 2 // a will be set to 2