Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<ol> <li><p>A pointer can be re-assigned: </p> <pre><code>int x = 5; int y = 6; int *p; p = &amp;x; p = &amp;y; *p = 10; assert(x == 5); assert(y == 10); </code></pre> <p>A reference cannot, and must be assigned at initialization:</p> <pre><code>int x = 5; int y = 6; int &amp;r = x; </code></pre></li> <li><p>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 <a href="https://stackoverflow.com/questions/79923/what-and-where-are-the-stack-and-heap#79936">stack vs heap</a>. This implies that there is a real address of a reference that the compiler will not tell you. </p> <pre><code>int x = 0; int &amp;r = x; int *p = &amp;x; int *p2 = &amp;r; assert(p == p2); </code></pre></li> <li><p>You can have pointers to pointers to pointers offering extra levels of indirection. Whereas references only offer one level of indirection. </p> <pre><code>int x = 0; int y = 0; int *p = &amp;x; int *q = &amp;y; int **pp = &amp;p; pp = &amp;q;//*pp = q **pp = 4; assert(y == 4); assert(x == 0); </code></pre></li> <li><p>Pointer can be assigned <code>nullptr</code> directly, whereas reference cannot. If you try hard enough, and you know how, you can make the address of a reference <code>nullptr</code>. Likewise, if you try hard enough you can have a reference to a pointer, and then that reference can contain <code>nullptr</code>.</p> <pre><code>int *p = nullptr; int &amp;r = nullptr; &lt;--- compiling error int &amp;r = *p; &lt;--- 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 </code></pre></li> <li><p>Pointers can iterate over an array, you can use <code>++</code> to go to the next item that a pointer is pointing to, and <code>+ 4</code> to go to the 5th element. This is no matter what size the object is that the pointer points to.</p></li> <li><p>A pointer needs to be dereferenced with <code>*</code> to access the memory location it points to, whereas a reference can be used directly. A pointer to a class/struct uses <code>-&gt;</code> to access it's members whereas a reference uses a <code>.</code>.</p></li> <li><p>A pointer is a variable that holds a memory address. Regardless of how a reference is implemented, a reference has the same memory address as the item it references.</p></li> <li><p>References cannot be stuffed into an array, whereas pointers can be (Mentioned by user @litb)</p></li> <li><p>Const references can be bound to temporaries. Pointers cannot (not without some indirection):</p> <pre><code>const int &amp;x = int(12); //legal C++ int *y = &amp;int(12); //illegal to dereference a temporary. </code></pre> <p>This makes <code>const&amp;</code> safer for use in argument lists and so forth.</p></li> </ol>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload