c pointers vs references

Solutions on MaxInterview for c pointers vs references by the best coders in the world

showing results for - "c pointers vs references"
Ramsey
03 Jan 2017
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.
queries leading to this page
c 2b 2b pointers and referencesdifference between reference and pointerreference vs pointer c 2b 2bpointers vs referencesc 2b 2b by reference vs pointerwhen to use a pointer vs referencewhy would you use a pointer vs a aliaspointer vs referencepointer reference in c 2b 2bc 2b 2b pointer or referencec 2b 2b pointers and referencescpp reference vs pointerdifference between ref and pointerpointer vs reference in c 2b 2bc 2b 2b pointer vs referencereferneces vs pointersc 2b 2b 26 vs 2apointer vs reference variable creferences and pointers c 2b 2bwhy would you use a pointer vs a alias c 2b 2bc 2b 2b why is 26 used for both references and pointersc 2b 2b reference to pointer variablepointer vs reference graphichow if ponter different form referencedifference in reference variable and pointer variablec 2b 2b references and pointerswhen to use pointers cppdifference between pointer and reference in c 2b 2breference and pointers cpointer refences cpppointers and references c 2b 2bc 2b 2b difference between a pointer and a referencereference pointercpp pointer vs referencepointers and reference in c 2b 2bc pointers 3e vs references vs pointerspointers vs references in cc 2b 2b pointers vs referencesreferences and pointers in c 2b 2breferences vs pointers c 2b 2bdifferences between references and pointerswhen and why use pointers and references in c 2b 2breference and pointers differencec pointer 2a vs 2a 2apointer reference c 2b 2breference vs pointer in cc pointer vs referencei vs pointer i in creference vs pointerwhat are the differences between a pointer variable and a reference variable in c 2b 2b 3fwhen to use pointers vs references c 2b 2bdifference between pointer and referencedifference between a reference and a pointerreferences vs pointers in c 2b 2bare pointers and references the same thingc 2b 2b address vs referencewhen should i use pointers in c 2b 2breferences in cpp vs cc 2b 2b why use pointers vs regular variablespointers and referencesc 2b 2b pointers and references explaineddifference between references and pointersc 2b 2b reference vs pointerare pointers and references the same in c 2b 2bc pointed vs referenceprogramming language where you must reference pointers 26 in c 2b 2bdifference between reference and pointer in c 2b 2bpointer vs reference cc reference vs pointerreference vs pointetrue or false reference variables are similar to pointer variables 2c but there are some important differences c and c 2b 2b both support pointer and references pointer vs reference c 2b 2bpointers and references in cppwhen use reference vs pointer object c 2b 2bpointers vs reference c 2b 2breference vs pointersc 2b 2b pointer vs thisreference to a pointer importancec 2b 2b pointer and referencereference and pointer c 2b 2bdifference between pointers and referencespointers and references in c 2b 2baddress vs reference c 2b 2bc pointers vs references