<p>What is the difference between a pointer variable and a reference variable?</p>

[{'answer_id': 57490, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 7, 'selected': False, 'text': "<p>Apart from syntactic sugar, a reference is a <code>const</code> pointer (<em>not</em> pointer to a <code>const</code>). You must establish what it refers to when you declare the reference variable, and you cannot change it later.</p>\n\n<p>Update: now that I think about it some more, there is an important difference.</p>\n\n<p>A const pointer's target can be replaced by taking its address and using a const cast.</p>\n\n<p>A reference's target cannot be replaced in any way short of UB.</p>\n\n<p>This should permit the compiler to do more optimization on a reference.</p>\n"}, {'answer_id': 57492, 'author': 'Brian R. Bondy', 'author_id': 3153, 'author_profile': 'https://Stackoverflow.com/users/3153', 'pm_score': 11, 'selected': False, 'text': '<ol>\n<li><p>A pointer can be re-assigned:</p>\n<pre><code>int x = 5;\nint y = 6;\nint *p;\np = &amp;x;\np = &amp;y;\n*p = 10;\nassert(x == 5);\nassert(y == 10);\n</code></pre>\n<p>A reference cannot be re-bound, and must be bound at initialization:</p>\n<pre><code>int x = 5;\nint y = 6;\nint &amp;q; // error\nint &amp;r = x;\n</code></pre>\n</li>\n<li><p>A pointer variable has its own identity: a distinct, visible memory address that can be taken with the unary <code>&amp;</code> operator and a certain amount of space that can be measured with the <code>sizeof</code> operator. Using those operators on a reference returns a value corresponding to whatever the reference is bound to; the reference’s own address and size are invisible. Since the reference assumes the identity of the original variable in this way, it is convenient to think of a reference as another name for the same variable.</p>\n<pre><code>int x = 0;\nint &amp;r = x;\nint *p = &amp;x;\nint *p2 = &amp;r;\n\nassert(p == p2); // &amp;x == &amp;r\nassert(&amp;p != &amp;p2);\n</code></pre>\n</li>\n<li><p>You can have arbitrarily nested pointers to pointers offering extra levels of indirection. References only offer one level of indirection.</p>\n<pre><code>int x = 0;\nint y = 0;\nint *p = &amp;x;\nint *q = &amp;y;\nint **pp = &amp;p;\n\n**pp = 2;\npp = &amp;q; // *pp is now q\n**pp = 4;\n\nassert(y == 4);\nassert(x == 2);\n</code></pre>\n</li>\n<li><p>A pointer can be assigned <code>nullptr</code>, whereas a reference must be bound to an existing object. If you try hard enough, you can bind a reference to <code>nullptr</code>, but this is <a href="https://stackoverflow.com/questions/2397984/">undefined</a> and will not behave consistently.</p>\n<pre><code>/* the code below is undefined; your compiler may optimise it\n * differently, emit warnings, or outright refuse to compile it */\n\nint &amp;r = *static_cast&lt;int *&gt;(nullptr);\n\n// prints &quot;null&quot; under GCC 10\nstd::cout\n    &lt;&lt; (&amp;r != nullptr\n        ? &quot;not null&quot; : &quot;null&quot;)\n    &lt;&lt; std::endl;\n\nbool f(int &amp;r) { return &amp;r != nullptr; }\n\n// prints &quot;not null&quot; under GCC 10\nstd::cout\n    &lt;&lt; (f(*static_cast&lt;int *&gt;(nullptr))\n        ? &quot;not null&quot; : &quot;null&quot;)\n    &lt;&lt; std::endl;\n</code></pre>\n<p>You can, however, have a reference to a pointer whose value is <code>nullptr</code>.</p>\n</li>\n<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>\n</li>\n<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 its members whereas a reference uses a <code>.</code>.</p>\n</li>\n<li><p>References cannot be put into an array, whereas pointers can be (Mentioned by user @litb)</p>\n</li>\n<li><p>Const references can be bound to temporaries. Pointers cannot (not without some indirection):</p>\n<pre><code>const int &amp;x = int(12); // legal C++\nint *y = &amp;int(12); // illegal to take the address of a temporary.\n</code></pre>\n<p>This makes <code>const &amp;</code> more convenient to use in argument lists and so forth.</p>\n</li>\n</ol>\n'}, {'answer_id': 57502, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 6, 'selected': False, 'text': '<p>A reference can never be <code>NULL</code>.</p>\n'}, {'answer_id': 57656, 'author': 'Mark Ransom', 'author_id': 5987, 'author_profile': 'https://Stackoverflow.com/users/5987', 'pm_score': 7, 'selected': False, 'text': '<p>Contrary to popular opinion, it is possible to have a reference that is NULL.</p>\n\n<pre><code>int * p = NULL;\nint &amp; r = *p;\nr = 1;  // crash! (if you\'re lucky)\n</code></pre>\n\n<p>Granted, it is much harder to do with a reference - but if you manage it, you\'ll tear your hair out trying to find it. References are <em>not</em> inherently safe in C++!</p>\n\n<p>Technically this is an <strong>invalid reference</strong>, not a null reference.  C++ doesn\'t support null references as a concept as you might find in other languages.  There are other kinds of invalid references as well. <em>Any</em> invalid reference raises the spectre of <strong>undefined behavior</strong>, just as using an invalid pointer would.</p>\n\n<p>The actual error is in the dereferencing of the NULL pointer, prior to the assignment to a reference.  But I\'m not aware of any compilers that will generate any errors on that condition - the error propagates to a point further along in the code. That\'s what makes this problem so insidious. Most of the time, if you dereference a NULL pointer, you crash right at that spot and it doesn\'t take much debugging to figure it out.</p>\n\n<p>My example above is short and contrived. Here\'s a more real-world example.</p>\n\n<pre><code>class MyClass\n{\n    ...\n    virtual void DoSomething(int,int,int,int,int);\n};\n\nvoid Foo(const MyClass &amp; bar)\n{\n    ...\n    bar.DoSomething(i1,i2,i3,i4,i5);  // crash occurs here due to memory access violation - obvious why?\n}\n\nMyClass * GetInstance()\n{\n    if (somecondition)\n        return NULL;\n    ...\n}\n\nMyClass * p = GetInstance();\nFoo(*p);\n</code></pre>\n\n<p>I want to reiterate that the only way to get a null reference is through malformed code, and once you have it you\'re getting undefined behavior. It <strong>never</strong> makes sense to check for a null reference; for example you can try <code>if(&amp;bar==NULL)...</code> but the compiler might optimize the statement out of existence! A valid reference can never be NULL so from the compiler\'s view the comparison is always false, and it is free to eliminate the <code>if</code> clause as dead code - this is the essence of undefined behavior.</p>\n\n<p>The proper way to stay out of trouble is to avoid dereferencing a NULL pointer to create a reference. Here\'s an automated way to accomplish this.</p>\n\n<pre><code>template&lt;typename T&gt;\nT&amp; deref(T* p)\n{\n    if (p == NULL)\n        throw std::invalid_argument(std::string("NULL reference"));\n    return *p;\n}\n\nMyClass * p = GetInstance();\nFoo(deref(p));\n</code></pre>\n\n<p>For an older look at this problem from someone with better writing skills, see <a href="http://www.gotw.ca/conv/002.htm" rel="noreferrer">Null References</a> from Jim Hyslop and Herb Sutter.</p>\n\n<p>For another example of the dangers of dereferencing a null pointer see <a href="https://blogs.msdn.microsoft.com/oldnewthing/20171222-00/?p=97635" rel="noreferrer">Exposing undefined behavior when trying to port code to another platform</a> by Raymond Chen.</p>\n'}, {'answer_id': 57734, 'author': 'Matt Price', 'author_id': 852, 'author_profile': 'https://Stackoverflow.com/users/852', 'pm_score': 8, 'selected': False, 'text': '<p>If you want to be really pedantic, there is one thing you can do with a reference that you can\'t do with a pointer: extend the lifetime of a temporary object. In C++ if you bind a const reference to a temporary object, the lifetime of that object becomes the lifetime of the reference.</p>\n\n<pre><code>std::string s1 = "123";\nstd::string s2 = "456";\n\nstd::string s3_copy = s1 + s2;\nconst std::string&amp; s3_reference = s1 + s2;\n</code></pre>\n\n<p>In this example s3_copy copies the temporary object that is a result of the concatenation. Whereas s3_reference in essence becomes the temporary object. It\'s really a reference to a temporary object that now has the same lifetime as the reference. </p>\n\n<p>If you try this without the <code>const</code> it should fail to compile. You cannot bind a non-const reference to a temporary object, nor can you take its address for that matter.</p>\n'}, {'answer_id': 57780, 'author': 'Orion Edwards', 'author_id': 234, 'author_profile': 'https://Stackoverflow.com/users/234', 'pm_score': 7, 'selected': False, 'text': '<p>You forgot the most important part:</p>\n\n<p>member-access with pointers uses <code>-&gt;</code> <br/>\nmember-access with references uses <code>.</code></p>\n\n<p><code>foo.bar</code> is <em>clearly</em> superior to <code>foo-&gt;bar</code> in the same way that <a href="http://en.wikipedia.org/wiki/Vi" rel="noreferrer">vi</a> is <em>clearly</em> superior to <a href="http://en.wikipedia.org/wiki/Emacs" rel="noreferrer">Emacs</a> :-)</p>\n'}, {'answer_id': 58996, 'author': 'Aardvark', 'author_id': 3655, 'author_profile': 'https://Stackoverflow.com/users/3655', 'pm_score': 4, 'selected': False, 'text': '<p>I use references unless I need either of these:</p>\n\n<ul>\n<li><p>Null pointers can be used as a\nsentinel value, often a cheap way to\navoid function overloading or use of\na bool.</p></li>\n<li><p>You can do arithmetic on a pointer.\nFor example, <code>p += offset;</code></p></li>\n</ul>\n'}, {'answer_id': 59636, 'author': 'Don Wakefield', 'author_id': 3778, 'author_profile': 'https://Stackoverflow.com/users/3778', 'pm_score': 4, 'selected': False, 'text': "<p>Another interesting use of references is to supply a default argument of a user-defined type:</p>\n\n<pre><code>class UDT\n{\npublic:\n   UDT() : val_d(33) {};\n   UDT(int val) : val_d(val) {};\n   virtual ~UDT() {};\nprivate:\n   int val_d;\n};\n\nclass UDT_Derived : public UDT\n{\npublic:\n   UDT_Derived() : UDT() {};\n   virtual ~UDT_Derived() {};\n};\n\nclass Behavior\n{\npublic:\n   Behavior(\n      const UDT &amp;udt = UDT()\n   )  {};\n};\n\nint main()\n{\n   Behavior b; // take default\n\n   UDT u(88);\n   Behavior c(u);\n\n   UDT_Derived ud;\n   Behavior d(ud);\n\n   return 1;\n}\n</code></pre>\n\n<p>The default flavor uses the 'bind const reference to a temporary' aspect of references.</p>\n"}, {'answer_id': 60148, 'author': 'MSN', 'author_id': 6210, 'author_profile': 'https://Stackoverflow.com/users/6210', 'pm_score': 5, 'selected': False, 'text': '<p>It doesn\'t matter how much space it takes up since you can\'t actually see any side effect (without executing code) of whatever space it would take up.</p>\n\n<p>On the other hand, one major difference between references and pointers is that temporaries assigned to const references live until the const reference goes out of scope.</p>\n\n<p>For example:</p>\n\n<pre><code>class scope_test\n{\npublic:\n    ~scope_test() { printf("scope_test done!\\n"); }\n};\n\n...\n\n{\n    const scope_test &amp;test= scope_test();\n    printf("in scope\\n");\n}\n</code></pre>\n\n<p>will print:</p>\n\n<pre><code>in scope\nscope_test done!\n</code></pre>\n\n<p>This is the language mechanism that allows ScopeGuard to work.</p>\n'}, {'answer_id': 101406, 'author': 'Vincent Robert', 'author_id': 268, 'author_profile': 'https://Stackoverflow.com/users/268', 'pm_score': 6, 'selected': False, 'text': '<p>Actually, a reference is not really like a pointer.</p>\n\n<p>A compiler keeps "references" to variables, associating a name with a memory address; that\'s its job to translate any variable name to a memory address when compiling.</p>\n\n<p>When you create a reference, you only tell the compiler that you assign another name to the pointer variable; that\'s why references cannot "point to null", because a variable cannot be, and not be.</p>\n\n<p>Pointers are variables; they contain the address of some other variable, or can be null. The important thing is that a pointer has a value, while a reference only has a variable that it is referencing.</p>\n\n<p>Now some explanation of real code:</p>\n\n<pre><code>int a = 0;\nint&amp; b = a;\n</code></pre>\n\n<p>Here you are not creating another variable that points to <code>a</code>; you are just adding another name to the memory content holding the value of <code>a</code>. This memory now has two names, <code>a</code> and <code>b</code>, and it can be addressed using either name.</p>\n\n<pre><code>void increment(int&amp; n)\n{\n    n = n + 1;\n}\n\nint a;\nincrement(a);\n</code></pre>\n\n<p>When calling a function, the compiler usually generates memory spaces for the arguments to be copied to. The function signature defines the spaces that should be created and gives the name that should be used for these spaces. Declaring a parameter as a reference just tells the compiler to use the input variable memory space instead of allocating a new memory space during the method call. It may seem strange to say that your function will be directly manipulating a variable declared in the calling scope, but remember that when executing compiled code, there is no more scope; there is just plain flat memory, and your function code could manipulate any variables.</p>\n\n<p>Now there may be some cases where your compiler may not be able to know the reference when compiling, like when using an extern variable. So a reference may or may not be implemented as a pointer in the underlying code. But in the examples I gave you, it will most likely not be implemented with a pointer.</p>\n'}, {'answer_id': 596750, 'author': 'Christoph', 'author_id': 48015, 'author_profile': 'https://Stackoverflow.com/users/48015', 'pm_score': 9, 'selected': False, 'text': '<h1>What\'s a C++ reference (<em>for C programmers</em>)</h1>\n\n<p>A <em>reference</em> can be thought of as a <em>constant pointer</em> (not to be confused with a pointer to a constant value!) with automatic indirection, ie the compiler will apply the <code>*</code> operator for you.</p>\n\n<p>All references must be initialized with a non-null value or compilation will fail. It\'s neither possible to get the address of a reference - the address operator will return the address of the referenced value instead - nor is it possible to do arithmetics on references.</p>\n\n<p>C programmers might dislike C++ references as it will no longer be obvious when indirection happens or if an argument gets passed by value or by pointer without looking at function signatures.</p>\n\n<p>C++ programmers might dislike using pointers as they are considered unsafe - although references aren\'t really any safer than constant pointers except in the most trivial cases - lack the convenience of automatic indirection and carry a different semantic connotation.</p>\n\n<p>Consider the following statement from the <a href="https://isocpp.org/wiki/faq/references#overview-refs" rel="noreferrer"><em>C++ FAQ</em></a>:</p>\n\n<blockquote>\n  <p>Even though a reference is often implemented using an address in the\n  underlying assembly language, please do <em>not</em> think of a reference as a\n  funny looking pointer to an object. A reference <em>is</em> the object. It is\n  not a pointer to the object, nor a copy of the object. It <em>is</em> the\n  object.</p>\n</blockquote>\n\n<p>But if a reference <em>really</em> were the object, how could there be dangling references? In unmanaged languages, it\'s impossible for references to be any \'safer\' than pointers - there generally just isn\'t a way to reliably alias values across scope boundaries!</p>\n\n<h1>Why I consider C++ references useful</h1>\n\n<p>Coming from a C background, C++ references may look like a somewhat silly concept, but one should still use them instead of pointers where possible: Automatic indirection <em>is</em> convenient, and references become especially useful when dealing with <a href="https://en.wikipedia.org/wiki/Resource_Acquisition_Is_Initialization" rel="noreferrer">RAII</a> - but not because of any perceived safety advantage, but rather because they make writing idiomatic code less awkward.</p>\n\n<p>RAII is one of the central concepts of C++, but it interacts non-trivially with copying semantics. Passing objects by reference avoids these issues as no copying is involved. If references were not present in the language, you\'d have to use pointers instead, which are more cumbersome to use, thus violating the language design principle that the best-practice solution should be easier than the alternatives.</p>\n'}, {'answer_id': 1569931, 'author': 'Adisak', 'author_id': 14904, 'author_profile': 'https://Stackoverflow.com/users/14904', 'pm_score': 4, 'selected': False, 'text': "<p>Also, a reference that is a parameter to a function that is inlined may be handled differently than a pointer.</p>\n\n<pre><code>void increment(int *ptrint) { (*ptrint)++; }\nvoid increment(int &amp;refint) { refint++; }\nvoid incptrtest()\n{\n    int testptr=0;\n    increment(&amp;testptr);\n}\nvoid increftest()\n{\n    int testref=0;\n    increment(testref);\n}\n</code></pre>\n\n<p>Many compilers when inlining the pointer version one will actually force a write to memory (we are taking the address explicitly).  However, they will leave the reference in a register which is more optimal.</p>\n\n<p>Of course, for functions that are not inlined the pointer and reference generate the same code and it's always better to pass intrinsics by value than by reference if they are not modified and returned by the function.</p>\n"}, {'answer_id': 2162825, 'author': 'kriss', 'author_id': 168465, 'author_profile': 'https://Stackoverflow.com/users/168465', 'pm_score': 4, 'selected': False, 'text': "<p>Another difference is that you can have pointers to a void type (and it means pointer to anything) but references to void are forbidden.</p>\n\n<pre><code>int a;\nvoid * p = &amp;a; // ok\nvoid &amp; p = a;  //  forbidden\n</code></pre>\n\n<p>I can't say I'm really happy with this particular difference. I would much prefer it would be allowed with the meaning reference to anything with an address and otherwise the same behavior for references. It would allow to define some equivalents of C library functions like memcpy using references.</p>\n"}, {'answer_id': 6076707, 'author': 'Kunal Vyas', 'author_id': 731433, 'author_profile': 'https://Stackoverflow.com/users/731433', 'pm_score': 5, 'selected': False, 'text': '<p>While both references and pointers are used to indirectly access another value, there are two important differences between references and pointers. The first is that a reference always refers to an object: It is an error to define a reference without initializing it. The behavior of assignment is the second important difference: Assigning to a reference changes the object to which the reference is bound; it does not rebind the reference to another object. Once initialized, a reference always refers to the same underlying object.</p>\n\n<p>Consider these two program fragments. In the first, we assign one pointer to another:</p>\n\n<pre><code>int ival = 1024, ival2 = 2048;\nint *pi = &amp;ival, *pi2 = &amp;ival2;\npi = pi2;    // pi now points to ival2\n</code></pre>\n\n<p>After the assignment, ival, the object addressed by pi remains unchanged. The assignment changes the value of pi, making it point to a different object. Now consider a similar program that assigns two references:</p>\n\n<pre><code>int &amp;ri = ival, &amp;ri2 = ival2;\nri = ri2;    // assigns ival2 to ival\n</code></pre>\n\n<p>This assignment changes ival, the value referenced by ri, and not the reference itself. After the assignment, the two references still refer to their original objects, and the value of those objects is now the same as well.</p>\n'}, {'answer_id': 9157689, 'author': 'Andrzej', 'author_id': 838509, 'author_profile': 'https://Stackoverflow.com/users/838509', 'pm_score': 4, 'selected': False, 'text': '<p>There is one fundamental difference between pointers and references that I didn\'t see anyone had mentioned: references enable pass-by-reference semantics in function arguments. Pointers, although it is not visible at first do not: they only provide pass-by-value semantics. This has been very nicely described in <a href="http://javadude.com/articles/passbyvalue.htm">this article</a>.</p>\n\n<p>Regards,\n&amp;rzej</p>\n'}, {'answer_id': 14112711, 'author': 'fatma.ekici', 'author_id': 1678760, 'author_profile': 'https://Stackoverflow.com/users/1678760', 'pm_score': 5, 'selected': False, 'text': '<p>A reference is an alias for another variable whereas a pointer holds the memory address of a variable. References are generally used as function parameters so that the passed object is not the copy but the object itself. </p>\n\n<pre><code>    void fun(int &amp;a, int &amp;b); // A common usage of references.\n    int a = 0;\n    int &amp;b = a; // b is an alias for a. Not so common to use. \n</code></pre>\n'}, {'answer_id': 15081923, 'author': 'tanweer alam', 'author_id': 2002964, 'author_profile': 'https://Stackoverflow.com/users/2002964', 'pm_score': 4, 'selected': False, 'text': "<p>A reference is not another name given to some memory. It's a immutable pointer that is automatically de-referenced on usage. Basically it boils down to:</p>\n\n<pre><code>int&amp; j = i;\n</code></pre>\n\n<p>It internally becomes</p>\n\n<pre><code>int* const j = &amp;i;\n</code></pre>\n"}, {'answer_id': 15423961, 'author': 'Arlene Batada', 'author_id': 2172349, 'author_profile': 'https://Stackoverflow.com/users/2172349', 'pm_score': 4, 'selected': False, 'text': '<p>This program might help in comprehending the answer of the question. This is a simple program of a reference "j" and a pointer "ptr" pointing to variable "x".</p>\n\n<pre><code>#include&lt;iostream&gt;\n\nusing namespace std;\n\nint main()\n{\nint *ptr=0, x=9; // pointer and variable declaration\nptr=&amp;x; // pointer to variable "x"\nint &amp; j=x; // reference declaration; reference to variable "x"\n\ncout &lt;&lt; "x=" &lt;&lt; x &lt;&lt; endl;\n\ncout &lt;&lt; "&amp;x=" &lt;&lt; &amp;x &lt;&lt; endl;\n\ncout &lt;&lt; "j=" &lt;&lt; j &lt;&lt; endl;\n\ncout &lt;&lt; "&amp;j=" &lt;&lt; &amp;j &lt;&lt; endl;\n\ncout &lt;&lt; "*ptr=" &lt;&lt; *ptr &lt;&lt; endl;\n\ncout &lt;&lt; "ptr=" &lt;&lt; ptr &lt;&lt; endl;\n\ncout &lt;&lt; "&amp;ptr=" &lt;&lt; &amp;ptr &lt;&lt; endl;\n    getch();\n}\n</code></pre>\n\n<p>Run the program and have a look at the output and you\'ll understand.</p>\n\n<p>Also, spare 10 minutes and watch this video: <a href="https://www.youtube.com/watch?v=rlJrrGV0iOg">https://www.youtube.com/watch?v=rlJrrGV0iOg</a></p>\n'}, {'answer_id': 18555015, 'author': 'Cort Ammon', 'author_id': 2728148, 'author_profile': 'https://Stackoverflow.com/users/2728148', 'pm_score': 6, 'selected': False, 'text': '<p>References are very similar to pointers, but they are specifically crafted to be helpful to optimizing compilers.</p>\n\n<ul>\n<li>References are designed such that it is substantially easier for the compiler to trace which reference aliases which variables. Two major features are very important: no "reference arithmetic" and no reassigning of references. These allow the compiler to figure out which references alias which variables at compile time.</li>\n<li>References are allowed to refer to variables which do not have memory addresses, such as those the compiler chooses to put into registers. If you take the address of a local variable, it is very hard for the compiler to put it in a register.</li>\n</ul>\n\n<p>As an example:</p>\n\n<pre><code>void maybeModify(int&amp; x); // may modify x in some way\n\nvoid hurtTheCompilersOptimizer(short size, int array[])\n{\n    // This function is designed to do something particularly troublesome\n    // for optimizers. It will constantly call maybeModify on array[0] while\n    // adding array[1] to array[2]..array[size-1]. There\'s no real reason to\n    // do this, other than to demonstrate the power of references.\n    for (int i = 2; i &lt; (int)size; i++) {\n        maybeModify(array[0]);\n        array[i] += array[1];\n    }\n}\n</code></pre>\n\n<p>An optimizing compiler may realize that we are accessing a[0] and a[1] quite a bunch. It would love to optimize the algorithm to:</p>\n\n<pre><code>void hurtTheCompilersOptimizer(short size, int array[])\n{\n    // Do the same thing as above, but instead of accessing array[1]\n    // all the time, access it once and store the result in a register,\n    // which is much faster to do arithmetic with.\n    register int a0 = a[0];\n    register int a1 = a[1]; // access a[1] once\n    for (int i = 2; i &lt; (int)size; i++) {\n        maybeModify(a0); // Give maybeModify a reference to a register\n        array[i] += a1;  // Use the saved register value over and over\n    }\n    a[0] = a0; // Store the modified a[0] back into the array\n}\n</code></pre>\n\n<p>To make such an optimization, it needs to prove that nothing can change array[1] during the call. This is rather easy to do. i is never less than 2, so array[i] can never refer to array[1]. maybeModify() is given a0 as a reference (aliasing array[0]). Because there is no "reference" arithmetic, the compiler just has to prove that maybeModify never gets the address of x, and it has proven that nothing changes array[1].</p>\n\n<p>It also has to prove that there are no ways a future call could read/write a[0] while we have a temporary register copy of it in a0. This is often trivial to prove, because in many cases it is obvious that the reference is never stored in a permanent structure like a class instance.</p>\n\n<p>Now do the same thing with pointers</p>\n\n<pre><code>void maybeModify(int* x); // May modify x in some way\n\nvoid hurtTheCompilersOptimizer(short size, int array[])\n{\n    // Same operation, only now with pointers, making the\n    // optimization trickier.\n    for (int i = 2; i &lt; (int)size; i++) {\n        maybeModify(&amp;(array[0]));\n        array[i] += array[1];\n    }\n}\n</code></pre>\n\n<p>The behavior is the same; only now it is much harder to prove that maybeModify does not ever modify array[1], because we already gave it a pointer; the cat is out of the bag. Now it has to do the much more difficult proof: a static analysis of maybeModify to prove it never writes to &amp;x + 1. It also has to prove that it never saves off a pointer that can refer to array[0], which is just as tricky.</p>\n\n<p>Modern compilers are getting better and better at static analysis, but it is always nice to help them out and use references.</p>\n\n<p>Of course, barring such clever optimizations, compilers will indeed turn references into pointers when needed.</p>\n\n<p>EDIT: Five years after posting this answer, I found an actual technical difference where references are different than just a different way of looking at the same addressing concept.  References can modify the lifespan of temporary objects in a way that pointers cannot.</p>\n\n<pre><code>F createF(int argument);\n\nvoid extending()\n{\n    const F&amp; ref = createF(5);\n    std::cout &lt;&lt; ref.getArgument() &lt;&lt; std::endl;\n};\n</code></pre>\n\n<p>Normally temporary objects such as the one created by the call to <code>createF(5)</code> are destroyed at the end of the expression.  However, by binding that object to a reference, <code>ref</code>, C++ will extend the lifespan of that temporary object until <code>ref</code> goes out of scope.</p>\n'}, {'answer_id': 21092239, 'author': 'Life', 'author_id': 2143209, 'author_profile': 'https://Stackoverflow.com/users/2143209', 'pm_score': 5, 'selected': False, 'text': '<p>This is based on the <a href="http://www.cplusplus.com/files/tutorial.pdf">tutorial</a>. What is written makes it more clear:</p>\n\n<pre><code>&gt;&gt;&gt; The address that locates a variable within memory is\n    what we call a reference to that variable. (5th paragraph at page 63)\n\n&gt;&gt;&gt; The variable that stores the reference to another\n    variable is what we call a pointer. (3rd paragraph at page 64)\n</code></pre>\n\n<p>Simply to remember that,</p>\n\n<pre><code>&gt;&gt;&gt; reference stands for memory location\n&gt;&gt;&gt; pointer is a reference container (Maybe because we will use it for\nseveral times, it is better to remember that reference.)\n</code></pre>\n\n<p>What\'s more, as we can refer to almost any pointer tutorial, a pointer is an object that is supported by pointer arithmetic which makes pointer similar to an array.</p>\n\n<p>Look at the following statement,</p>\n\n<pre><code>int Tom(0);\nint &amp; alias_Tom = Tom;\n</code></pre>\n\n<p><code>alias_Tom</code> can be understood as an <code>alias of a variable</code> (different with <code>typedef</code>, which is <code>alias of a type</code>) <code>Tom</code>. It is also OK to forget the terminology of such statement is to create a reference of <code>Tom</code>.</p>\n'}, {'answer_id': 26370807, 'author': 'Tory', 'author_id': 3093272, 'author_profile': 'https://Stackoverflow.com/users/3093272', 'pm_score': 4, 'selected': False, 'text': '<p>At the risk of adding to confusion, I want to throw in some input, I\'m sure it mostly depends on how the compiler implements references, but in the case of gcc the idea that a reference can only point to a variable on the stack is not actually correct, take this for example:</p>\n\n<pre><code>#include &lt;iostream&gt;\nint main(int argc, char** argv) {\n    // Create a string on the heap\n    std::string *str_ptr = new std::string("THIS IS A STRING");\n    // Dereference the string on the heap, and assign it to the reference\n    std::string &amp;str_ref = *str_ptr;\n    // Not even a compiler warning! At least with gcc\n    // Now lets try to print it\'s value!\n    std::cout &lt;&lt; str_ref &lt;&lt; std::endl;\n    // It works! Now lets print and compare actual memory addresses\n    std::cout &lt;&lt; str_ptr &lt;&lt; " : " &lt;&lt; &amp;str_ref &lt;&lt; std::endl;\n    // Exactly the same, now remember to free the memory on the heap\n    delete str_ptr;\n}\n</code></pre>\n\n<p>Which outputs this:</p>\n\n<pre><code>THIS IS A STRING\n0xbb2070 : 0xbb2070\n</code></pre>\n\n<p>If you notice even the memory addresses are exactly the same, meaning the reference is successfully pointing to a variable on the heap! Now if you really want to get freaky, this also works:</p>\n\n<pre><code>int main(int argc, char** argv) {\n    // In the actual new declaration let immediately de-reference and assign it to the reference\n    std::string &amp;str_ref = *(new std::string("THIS IS A STRING"));\n    // Once again, it works! (at least in gcc)\n    std::cout &lt;&lt; str_ref;\n    // Once again it prints fine, however we have no pointer to the heap allocation, right? So how do we free the space we just ignorantly created?\n    delete &amp;str_ref;\n    /*And, it works, because we are taking the memory address that the reference is\n    storing, and deleting it, which is all a pointer is doing, just we have to specify\n    the address with \'&amp;\' whereas a pointer does that implicitly, this is sort of like\n    calling delete &amp;(*str_ptr); (which also compiles and runs fine).*/\n}\n</code></pre>\n\n<p>Which outputs this:</p>\n\n<pre><code>THIS IS A STRING\n</code></pre>\n\n<p>Therefore a reference IS a pointer under the hood, they both are just storing a memory address, where the address is pointing to is irrelevant, what do you think would happen if I called std::cout &lt;&lt; str_ref; AFTER calling delete &amp;str_ref? Well, obviously it compiles fine, but causes a segmentation fault at runtime because it\'s no longer pointing at a valid variable, we essentially have a broken reference that still exists (until it falls out of scope), but is useless.</p>\n\n<p>In other words, a reference is nothing but a pointer that has the pointer mechanics abstracted away, making it safer and easier to use (no accidental pointer math, no mixing up \'.\' and \'->\', etc.), assuming you don\'t try any nonsense like my examples above ;)</p>\n\n<p>Now <strong>regardless</strong> of how a compiler handles references, it will <strong>always</strong> have some kind of pointer under the hood, because a reference <strong>must</strong> refer to a specific variable at a specific memory address for it to work as expected, there is no getting around this (hence the term \'reference\').</p>\n\n<p>The only major rule that\'s important to remember with references is that they must be defined at the time of declaration (with the exception of a reference in a header, in that case it must be defined in the constructor, after the object it\'s contained in is constructed it\'s too late to define it).</p>\n\n<p><strong>Remember, my examples above are just that, examples demonstrating what a reference is, you would never want to use a reference in those ways! For proper usage of a reference there are plenty of answers on here already that hit the nail on the head</strong></p>\n'}, {'answer_id': 26636769, 'author': 'Lightness Races in Orbit', 'author_id': 560648, 'author_profile': 'https://Stackoverflow.com/users/560648', 'pm_score': 5, 'selected': False, 'text': '<p>There is a semantic difference that may appear esoteric if you are not familiar with studying computer languages in an abstract or even academic fashion.</p>\n\n<p>At the highest-level, the idea of references is that they are transparent "aliases". Your computer may use an address to make them work, but you\'re not supposed to worry about that: you\'re supposed to think of them as "just another name" for an existing object and the syntax reflects that. They are stricter than pointers so your compiler can more reliably warn you when you about to create a dangling reference, than when you are about to create a dangling pointer.</p>\n\n<p>Beyond that, there are of course some practical differences between pointers and references. The syntax to use them is obviously different, and you cannot "re-seat" references, have references to nothingness, or have pointers to references.</p>\n'}, {'answer_id': 27667467, 'author': 'George R', 'author_id': 289442, 'author_profile': 'https://Stackoverflow.com/users/289442', 'pm_score': 4, 'selected': False, 'text': '<p>Maybe some metaphors will help; \nIn the context of your desktop screenspace -   </p>\n\n<ul>\n<li>A reference requires you to specify an actual window.</li>\n<li>A pointer requires the location of a piece of space on screen that you assure it will contain zero or more instances of that window type.</li>\n</ul>\n'}, {'answer_id': 28410732, 'author': 'Destructor', 'author_id': 3777958, 'author_profile': 'https://Stackoverflow.com/users/3777958', 'pm_score': 4, 'selected': False, 'text': '<p>A reference to a pointer is possible in C++, but the reverse is not possible means a pointer to a reference isn\'t possible. A reference to a pointer provides a cleaner syntax to modify the pointer.\nLook at this example:</p>\n\n<pre><code>#include&lt;iostream&gt;\nusing namespace std;\n\nvoid swap(char * &amp;str1, char * &amp;str2)\n{\n  char *temp = str1;\n  str1 = str2;\n  str2 = temp;\n}\n\nint main()\n{\n  char *str1 = "Hi";\n  char *str2 = "Hello";\n  swap(str1, str2);\n  cout&lt;&lt;"str1 is "&lt;&lt;str1&lt;&lt;endl;\n  cout&lt;&lt;"str2 is "&lt;&lt;str2&lt;&lt;endl;\n  return 0;\n}\n</code></pre>\n\n<p>And consider the C version of the above program. In C you have to use pointer to pointer (multiple indirection), and it leads to confusion and the program may look complicated.</p>\n\n<pre><code>#include&lt;stdio.h&gt;\n/* Swaps strings by swapping pointers */\nvoid swap1(char **str1_ptr, char **str2_ptr)\n{\n  char *temp = *str1_ptr;\n  *str1_ptr = *str2_ptr;\n  *str2_ptr = temp;\n}\n\nint main()\n{\n  char *str1 = "Hi";\n  char *str2 = "Hello";\n  swap1(&amp;str1, &amp;str2);\n  printf("str1 is %s, str2 is %s", str1, str2);\n  return 0;\n}\n</code></pre>\n\n<p>Visit the following for more information about reference to pointer:</p>\n\n<ul>\n<li><em><a href="http://markgodwin.blogspot.in/2009/08/c-reference-to-pointer.html">C++: Reference to Pointer</a></em></li>\n<li><em><a href="http://www.codeguru.com/cpp/cpp/cpp_mfc/pointers/article.php/c4089/PointertoPointer-and-ReferencetoPointer.htm">Pointer-to-Pointer and Reference-to-Pointer</a></em></li>\n</ul>\n\n<p>As I said, a pointer to a reference isn\'t possible. Try the following program:</p>\n\n<pre><code>#include &lt;iostream&gt;\nusing namespace std;\n\nint main()\n{\n   int x = 10;\n   int *ptr = &amp;x;\n   int &amp;*ptr1 = ptr;\n}\n</code></pre>\n'}, {'answer_id': 36823688, 'author': 'Zorgiev', 'author_id': 1466198, 'author_profile': 'https://Stackoverflow.com/users/1466198', 'pm_score': 3, 'selected': False, 'text': "<p>The difference is that non-constant pointer variable(not to be confused with a pointer to constant) may be changed at some time during program execution, requires pointer semantics to be used(&amp;,*) operators, while references can be set upon initialization only(that's why you can set them in constructor initializer list only, but not somehow else) and use ordinary value accessing semantics.  Basically references were introduced to allow support for operators overloading as I had read in some very old book. As somebody stated in this thread - pointer can be set to 0 or whatever value you want. 0(NULL, nullptr) means that the pointer is initialized with nothing. It is an error to dereference null pointer. But actually the pointer may contain a value that doesn't point to some correct memory location. References in their turn try not to allow a user to initialize a reference to something that cannot be referenced due to the fact that you always provide rvalue of correct type to it. Although there are a lot of ways to make reference variable be initialized to a wrong memory location - it is better for you not to dig this deep into details. On machine level both pointer and reference work uniformly - via pointers. Let's say in essential references are syntactic sugar. rvalue references are different to this - they are naturally stack/heap objects.</p>\n"}, {'answer_id': 41507371, 'author': 'dhokar.w', 'author_id': 7383437, 'author_profile': 'https://Stackoverflow.com/users/7383437', 'pm_score': 3, 'selected': False, 'text': '<h3>Difference between pointer and reference</h3>\n<p>A pointer can be initialized to 0 and a reference not. In fact, a reference must also refer to an object, but a pointer can be the null pointer:</p>\n<pre><code>int* p = 0;\n</code></pre>\n<p>But we can’t have <code>int&amp; p = 0;</code> and also <code>int&amp; p=5 ;</code>.</p>\n<p>In fact to do it properly, we must have declared and defined an object at the first then we can make a reference to that object, so the correct implementation of the previous code will be:</p>\n<pre><code>Int x = 0;\nInt y = 5;\nInt&amp; p = x;\nInt&amp; p1 = y;\n</code></pre>\n<p>Another important point is that is we can make the declaration of the pointer without initialization however no such thing can be done in case of reference which must make a reference always to variable or object. However such use of a pointer is risky so generally we check if the pointer is actually is pointing to something or not. In case of a reference no such check is necessary, because we know already that referencing to an object during declaration is mandatory.</p>\n<p>Another difference is that pointer can point to another object however reference is always referencing to the same object, let’s take this example:</p>\n<pre><code>Int a = 6, b = 5;\nInt&amp; rf = a;\n\nCout &lt;&lt; rf &lt;&lt; endl; // The result we will get is 6, because rf is referencing to the value of a.\n\nrf = b;\ncout &lt;&lt; a &lt;&lt; endl; // The result will be 5 because the value of b now will be stored into the address of a so the former value of a will be erased\n</code></pre>\n<p>Another point: When we have a template like an STL template such kind of a class template will always return a reference, not a pointer, to make easy reading or assigning new value using operator []:</p>\n<pre><code>Std ::vector&lt;int&gt;v(10); // Initialize a vector with 10 elements\nV[5] = 5; // Writing the value 5 into the 6 element of our vector, so if the returned type of operator [] was a pointer and not a reference we should write this *v[5]=5, by making a reference we overwrite the element by using the assignment &quot;=&quot;\n</code></pre>\n'}, {'answer_id': 44957687, 'author': 'Ap31', 'author_id': 6350858, 'author_profile': 'https://Stackoverflow.com/users/6350858', 'pm_score': 4, 'selected': False, 'text': "<p>I feel like there is yet another point that hasn't been covered here.  </p>\n\n<p>Unlike the pointers, references are <strong>syntactically equivalent</strong> to the object they refer to, i.e. any operation that can be applied to an object works for a reference, and with the exact same syntax (the exception is of course the initialization).  </p>\n\n<p>While this may appear superficial, I believe this property is crucial for a number of C++ features, for example:  </p>\n\n<ul>\n<li><p><em>Templates</em>. Since template parameters are duck-typed, syntactic properties of a type is all that matters, so often the same template can be used with both <code>T</code> and <code>T&amp;</code>.<br>\n(or <code>std::reference_wrapper&lt;T&gt;</code> which still relies on an implicit cast\nto <code>T&amp;</code>)<br>\nTemplates that cover both <code>T&amp;</code> and <code>T&amp;&amp;</code> are even more common.   </p></li>\n<li><p><em>Lvalues</em>. Consider the statement <code>str[0] = 'X';</code> Without references it would only work for c-strings (<code>char* str</code>). Returning the character by reference allows user-defined classes to have the same notation.  </p></li>\n<li><p><em>Copy constructors</em>. Syntactically it makes sense to pass objects to copy constructors, and not pointers to objects. But there is just no way for a copy constructor to take an object by value - it would result in a recursive call to the same copy constructor. This leaves references as the only option here.  </p></li>\n<li><p><em>Operator overloads</em>. With references it is possible to introduce indirection to an operator call - say, <code>operator+(const T&amp; a, const T&amp; b)</code> while retaining the same infix notation. This also works for regular overloaded functions.  </p></li>\n</ul>\n\n<p>These points empower a considerable part of C++ and the standard library so this is quite a major property of references.</p>\n"}, {'answer_id': 46829217, 'author': 'Hitokage', 'author_id': 3027604, 'author_profile': 'https://Stackoverflow.com/users/3027604', 'pm_score': 2, 'selected': False, 'text': '<p>I always decide by <a href="https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#f60-prefer-t-over-t-when-no-argument-is-a-valid-option" rel="nofollow noreferrer">this</a> rule from C++ Core Guidelines:</p>\n\n<blockquote>\n  <p>Prefer T* over T&amp; when "no argument" is a valid option</p>\n</blockquote>\n'}, {'answer_id': 47073963, 'author': 'Arthur Tacca', 'author_id': 7008416, 'author_profile': 'https://Stackoverflow.com/users/7008416', 'pm_score': 4, 'selected': False, 'text': '<p>There is a very important non-technical difference between pointers and references: An argument passed to a function by pointer is much more visible than an argument passed to a function by non-const reference. For example:</p>\n\n<pre><code>void fn1(std::string s);\nvoid fn2(const std::string&amp; s);\nvoid fn3(std::string&amp; s);\nvoid fn4(std::string* s);\n\nvoid bar() {\n    std::string x;\n    fn1(x);  // Cannot modify x\n    fn2(x);  // Cannot modify x (without const_cast)\n    fn3(x);  // CAN modify x!\n    fn4(&amp;x); // Can modify x (but is obvious about it)\n}\n</code></pre>\n\n<p>Back in C, a call that looks like <code>fn(x)</code> can only be passed by value, so it definitely cannot modify <code>x</code>; to modify an argument you would need to pass a pointer <code>fn(&amp;x)</code>. So if an argument wasn\'t preceded by an <code>&amp;</code> you knew it would not be modified. (The converse, <code>&amp;</code> means modified, was not true because you would sometimes have to pass large read-only structures by <code>const</code> pointer.)</p>\n\n<p>Some argue that this is such a useful feature when reading code, that pointer parameters should always be used for modifiable parameters rather than non-<code>const</code> references, even if the function never expects a <code>nullptr</code>. That is, those people argue that function signatures like <code>fn3()</code> above should not be allowed. <a href="http://google.github.io/styleguide/cppguide.html#Reference_Arguments" rel="noreferrer">Google\'s C++ style guidelines</a> are an example of this.</p>\n'}, {'answer_id': 47243714, 'author': 'Immac', 'author_id': 3203817, 'author_profile': 'https://Stackoverflow.com/users/3203817', 'pm_score': 2, 'selected': False, 'text': "<p>I have an analogy for references and pointers, think of references as another name for an object and pointers as the address of an object.</p>\n\n<pre><code>// receives an alias of an int, an address of an int and an int value\npublic void my_function(int&amp; a,int* b,int c){\n    int d = 1; // declares an integer named d\n    int &amp;e = d; // declares that e is an alias of d\n    // using either d or e will yield the same result as d and e name the same object\n    int *f = e; // invalid, you are trying to place an object in an address\n    // imagine writting your name in an address field \n    int *g = f; // writes an address to an address\n    g = &amp;d; // &amp;d means get me the address of the object named d you could also\n    // use &amp;e as it is an alias of d and write it on g, which is an address so it's ok\n}\n</code></pre>\n"}, {'answer_id': 50058587, 'author': 'Michael Zheng', 'author_id': 1673396, 'author_profile': 'https://Stackoverflow.com/users/1673396', 'pm_score': 2, 'selected': False, 'text': '<p>Taryn♦ said:</p>\n\n<blockquote>\n  <p>You can\'t take the address of a reference like you can with pointers.</p>\n</blockquote>\n\n<p>Actually you can.</p>\n\n<p>I\'m quoting from <a href="https://stackoverflow.com/a/1950826">an answer on another question</a>:</p>\n\n<blockquote>\n  <p><a href="https://isocpp.org/wiki/faq/references" rel="nofollow noreferrer">The C++ FAQ</a> says it best:</p>\n  \n  <p>Unlike a pointer, once a reference is bound to an object, it can not be "reseated" to another object. The reference itself isn\'t an object (it has no identity; taking the address of a reference gives you the address of the referent; remember: the reference is its referent).</p>\n</blockquote>\n'}, {'answer_id': 51658995, 'author': 'Mark Lakata', 'author_id': 364818, 'author_profile': 'https://Stackoverflow.com/users/364818', 'pm_score': 2, 'selected': False, 'text': '<p>You can use the difference between references and pointers if you follow a convention for arguments passed to a function.  Const references are for data passed into a function, and pointers are for data passed out of a function.  In other languages, you can explicit notate this with keywords such as <code>in</code> and <code>out</code>. In C++, you can declare (by convention) the equivalent. For example,</p>\n\n<pre><code>void DoSomething(const Foo&amp; thisIsAnInput, Foo* thisIsAnOutput)\n{\n   if (thisIsAnOuput)\n      *thisIsAnOutput = thisIsAnInput;\n}\n</code></pre>\n\n<p>The use of references as inputs and pointers as outputs is part of the <a href="https://google.github.io/styleguide/cppguide.html#Output_Parameters" rel="noreferrer">Google style guide</a>.</p>\n'}, {'answer_id': 54222315, 'author': 'ebasconp', 'author_id': 1680261, 'author_profile': 'https://Stackoverflow.com/users/1680261', 'pm_score': 2, 'selected': False, 'text': '<p>Beside all the answers here,</p>\n\n<p>you can implement operator overloading using references:</p>\n\n<pre><code>my_point operator+(const my_point&amp; a, const my_point&amp; b)\n{\n  return { a.x + b.x, a.y + b.y };\n}\n</code></pre>\n\n<p>Using parameters as value would create temporary copies of the original arguments and using pointers would not invoke this function because of pointers arithmetics.</p>\n'}, {'answer_id': 54731129, 'author': 'FrankHB', 'author_id': 2307646, 'author_profile': 'https://Stackoverflow.com/users/2307646', 'pm_score': 5, 'selected': False, 'text': '<h2>The direct answer</h2>\n\n<p>What is a reference in C++? Some specific instance of type that <strong>is not an object type</strong>.</p>\n\n<p>What is a pointer in C++? Some specific instance of type that <strong>is an object type</strong>.</p>\n\n<p>From <a href="http://eel.is/c++draft/basic.types#8" rel="noreferrer">the ISO C++ definition of object type</a>:</p>\n\n<blockquote>\n  <p>An <em>object</em> type is a (possibly <em>cv</em>-qualified) type that is not a function type, not a reference type, and not <em>cv</em> void.</p>\n</blockquote>\n\n<p>It may be important to know, object type is a top-level category of the type universe in C++. Reference is also a top-level category. <strong>But pointer is not.</strong></p>\n\n<p>Pointers and references are mentioned together <a href="http://eel.is/c++draft/basic.compound#1" rel="noreferrer">in the context of <em>compound type</em></a>. This is basically due to the nature of the declarator syntax inherited from (and extended) C, which has no references. (Besides, there are more than one kind of declarator of references since C++ 11, while pointers are still "unityped": <code>&amp;</code>+<code>&amp;&amp;</code> vs. <code>*</code>.) So drafting a language specific by "extension" with similar style of C in this context is somewhat reasonable. (I will still argue that the syntax of declarators wastes the syntactic expressiveness <em>a lot</em>, makes both human users and implementations frustrating. Thus, all of them are not qualified to be <em>built-in</em> in a new language design. This is a totally different topic about PL design, though.)</p>\n\n<p>Otherwise, it is insignificant that pointers can be qualified as a specific sorts of types with references together. They simply share too few common properties besides the syntax similarity, so there is no need to put them together in most cases.</p>\n\n<p>Note the statements above only mentions "pointers" and "references" as types. There are some interested questions about their instances (like variables). There also come too many misconceptions.</p>\n\n<p>The differences of the top-level categories can already reveal many concrete differences not tied to pointers directly:</p>\n\n<ul>\n<li>Object types can have top-level <code>cv</code> qualifiers. References cannot.</li>\n<li>Variable of object types do occupy storage as per <a href="http://eel.is/c++draft/intro.abstract" rel="noreferrer">the abstract machine</a> semantics. Reference do not necessary occupy storage (see the section about misconceptions below for details).</li>\n<li>...</li>\n</ul>\n\n<p>A few more special rules on references:</p>\n\n<ul>\n<li><a href="http://eel.is/c++draft/dcl.ref#5" rel="noreferrer">Compound declarators are more restrictive on references.</a></li>\n<li>References can <a href="http://eel.is/c++draft/dcl.ref#6" rel="noreferrer">collapse</a>.\n\n<ul>\n<li>Special rules on <code>&amp;&amp;</code> parameters (as the "forwarding references") based on reference collapsing during template parameter deduction allow <a href="https://stackoverflow.com/questions/3582001">"perfect forwarding"</a> of parameters.</li>\n</ul></li>\n<li>References have special rules in initialization. The lifetime of variable declared as a reference type can be different to ordinary objects via extension.\n\n<ul>\n<li>BTW, a few other contexts like initialization involving <code>std::initializer_list</code> follows some similar rules of reference lifetime extension. It is another can of worms.</li>\n</ul></li>\n<li>...</li>\n</ul>\n\n<h2>The misconceptions</h2>\n\n<h3><a href="https://en.wikipedia.org/wiki/Syntactic_sugar" rel="noreferrer">Syntactic sugar</a></h3>\n\n<blockquote>\n  <p>I know references are syntactic sugar, so code is easier to read and write.</p>\n</blockquote>\n\n<p>Technically, this is plain wrong. References are not syntactic sugar of any other features in C++, because they cannot be exactly replaced by other features without any semantic differences.</p>\n\n<p>(Similarly, <em>lambda-expression</em>s are <em>not</em> syntactic sugar of any other features in C++ because it cannot be precisely simulated with "unspecified" properties like <a href="http://eel.is/c++draft/expr.prim.lambda#capture-9" rel="noreferrer">the declaration order of the captured variables</a>, which may be important because the initialization order of such variables can be significant.)</p>\n\n<p>C++ only has a few kinds of syntactic sugars in this strict sense. One instance is (inherited from C) the built-in (non-overloaded) operator <code>[]</code>, which <a href="http://eel.is/c++draft/expr.sub#1" rel="noreferrer">is defined exactly having same semantic properties of specific forms of combination over built-in operator unary <code>*</code> and binary <code>+</code></a>.</p>\n\n<h3>Storage</h3>\n\n<blockquote>\n  <p><strong><em>So, a pointer and a reference both use the same amount of memory.</em></strong></p>\n</blockquote>\n\n<p>The statement above is simply wrong. To avoid such misconceptions, look at the ISO C++ rules instead:</p>\n\n<p>From <a href="http://eel.is/c++draft/intro.object#1" rel="noreferrer">[intro.object]/1</a>:</p>\n\n<blockquote>\n  <p>... An object occupies a region of storage in its period of construction, throughout its lifetime, and in its period of destruction. ...</p>\n</blockquote>\n\n<p>From <a href="http://eel.is/c++draft/dcl.ref#4" rel="noreferrer">[dcl.ref]/4</a>:</p>\n\n<blockquote>\n  <p>It is unspecified whether or not a reference requires storage.</p>\n</blockquote>\n\n<p>Note these are <em>semantic</em> properties.</p>\n\n<h3>Pragmatics</h3>\n\n<p>Even that pointers are not qualified enough to be put together with references in the sense of the language design, there are still some arguments making it debatable to make choice between them in some other contexts, for example, when making choices on parameter types.</p>\n\n<p>But this is not the whole story. I mean, there are more things than pointers vs references you have to consider.</p>\n\n<p>If you don\'t have to stick on such over-specific choices, in most cases the answer is short: <strong>you do not have the necessity to use pointers, so you don\'t</strong>. Pointers are usually bad enough because they imply too many things you don\'t expect and they will rely on too many implicit assumptions undermining the maintainability and (even) portability of the code. <strong>Unnecessarily relying on pointers is definitely a bad style and it should be avoided in the sense of modern C++.</strong> Reconsider your purpose and you will finally find that <strong>pointer is the feature of last sorts</strong> in most cases.</p>\n\n<ul>\n<li>Sometimes the language rules explicitly require specific types to be used. If you want to use these features, obey the rules.\n\n<ul>\n<li>Copy constructors require specific types of <em>cv</em>-<code>&amp;</code> reference type as the 1st parameter type. (And usually it should be <code>const</code> qualified.)</li>\n<li>Move constructors require specific types of <em>cv</em>-<code>&amp;&amp;</code> reference type as the 1st parameter type. (And usually there should be no qualifiers.)</li>\n<li>Specific overloads of operators require reference or non reference types. For example:\n\n<ul>\n<li>Overloaded <code>operator=</code> as special member functions requires reference types similar to 1st parameter of copy/move constructors.</li>\n<li>Postfix <code>++</code> requires dummy <code>int</code>.</li>\n<li>...</li>\n</ul></li>\n</ul></li>\n<li>If you know pass-by-value (i.e. using non-reference types) is sufficient, use it directly, particularly when using an implementation supporting C++17 mandated copy elision. (<strong>Warning</strong>: However, to <strong>exhaustively</strong> reason about the necessity can be <a href="https://stackoverflow.com/a/53825424">very complicated</a>.)</li>\n<li>If you want to operate some handles with ownership, use smart pointers like <code>unique_ptr</code> and <code>shared_ptr</code> (or even with homebrew ones by yourself if you require them to be <em>opaque</em>), rather than raw pointers.</li>\n<li>If you are doing some iterations over a range, use iterators (or some ranges which are not provided by the standard library yet), rather than raw pointers unless you are convinced raw pointers will do better (e.g. for less header dependencies) in very specific cases.</li>\n<li>If you know pass-by-value is sufficient and you want some explicit nullable semantics, use wrapper like <code>std::optional</code>, rather than raw pointers.</li>\n<li>If you know pass-by-value is not ideal for the reasons above, and you don\'t want nullable semantics, use {lvalue, rvalue, forwarding}-references.</li>\n<li>Even when you do want semantics like traditional pointer, there are often something more appropriate, like <code>observer_ptr</code> in Library Fundamental TS.</li>\n</ul>\n\n<p>The only exceptions cannot be worked around in the current language:</p>\n\n<ul>\n<li>When you are implementing smart pointers above, you may have to deal with raw pointers.</li>\n<li>Specific language-interoperation routines require pointers, like <code>operator new</code>. (However, <em>cv</em>-<code>void*</code> is still quite different and safer compared to the ordinary object pointers because it rules out unexpected pointer arithmetics unless you are relying on some non conforming extension on <code>void*</code> like GNU\'s.)</li>\n<li>Function pointers can be converted from lambda expressions without captures, while function references cannot. You have to use function pointers in non-generic code for such cases, even you deliberately do not want nullable values.</li>\n</ul>\n\n<p>So, in practice, the answer is so obvious: <strong>when in doubt, avoid pointers</strong>. You have to use pointers only when there are very explicit reasons that nothing else is more appropriate. Except a few exceptional cases mentioned above, such choices are almost always not purely C++-specific (but likely to be language-implementation-specific). Such instances can be:</p>\n\n<ul>\n<li>You have to serve to old-style (C) APIs.</li>\n<li>You have to meet the ABI requirements of specific C++ implementations.</li>\n<li>You have to interoperate at runtime with different language implementations (including various assemblies, language runtime and FFI of some high-level client languages) based on assumptions of specific implementations.</li>\n<li>You have to improve efficiency of the translation (compilation &amp; linking) in some extreme cases.</li>\n<li>You have to avoid symbol bloat in some extreme cases.</li>\n</ul>\n\n<h3>Language neutrality caveats</h3>\n\n<p>If you come to see the question via <a href="https://www.google.com/search?q=pointer+vs+reference&amp;hl=en" rel="noreferrer">some Google search result (not specific to C++)</a>, this is very likely to be the wrong place.</p>\n\n<p>References in C++ is quite "odd", as it is essentially not first-class: <a href="http://eel.is/c++draft/expr#type-1" rel="noreferrer">they will be treated as the objects or the functions being referred to</a> so they have no chance to support some first-class operations like being the left operand of <a href="http://eel.is/c++draft/expr.ref" rel="noreferrer">the member access operator</a> independently to the type of the referred object. Other languages may or may not have similar restrictions on their references.</p>\n\n<p>References in C++ will likely not preserve the meaning across different languages. For example, references in general do not imply nonnull properties on values like they in C++, so such assumptions may not work in some other languages (and you will find counterexamples quite easily, e.g. Java, C#, ...). </p>\n\n<p>There can still be some common properties among references in different programming languages in general, but let\'s leave it for some other questions in SO.</p>\n\n<p>(A side note: the question may be significant earlier than any "C-like" languages are involved, like <a href="https://academic.oup.com/comjnl/article-pdf/17/4/325/1215152/17-4-325.pdf" rel="noreferrer">ALGOL 68 vs. PL/I</a>.)</p>\n'}, {'answer_id': 57304404, 'author': 'Xitalogy', 'author_id': 1691559, 'author_profile': 'https://Stackoverflow.com/users/1691559', 'pm_score': 3, 'selected': False, 'text': '<h2>Some key pertinent details about references and pointers</h2>\n<h3>Pointers</h3>\n<ul>\n<li>Pointer variables are declared using the <em>unary suffix declarator operator</em> *</li>\n<li>Pointer objects are assigned an address value, for example, by assignment to an array object, the address of an object using the &amp; <em>unary prefix operator</em>, or assignment to the value of another pointer object</li>\n<li>A pointer can be reassigned any number of times, pointing to different objects</li>\n<li>A pointer is a variable that holds the assigned address.  It takes up storage in memory equal to the size of the address for the target machine architecture</li>\n<li>A pointer can be mathematically manipulated, for instance, by the increment or addition operators.  Hence, one can iterate with a pointer, etc.</li>\n<li>To get or set the contents of the object referred to by a pointer, one must use the <em>unary prefix operator</em> * to <em>dereference</em> it</li>\n</ul>\n<h3>References</h3>\n<ul>\n<li>References must be initialized when they are declared.</li>\n<li>References are declared using the <em>unary suffix declarator operator</em> &amp;.</li>\n<li>When initializing a reference, one uses the name of the object to which they will refer directly, without the need for the <em>unary prefix operator</em> &amp;</li>\n<li>Once initialized, references cannot be pointed to something else by assignment or arithmetical manipulation</li>\n<li>There is no need to dereference the reference to get or set the contents of the object it refers to</li>\n<li>Assignment operations on the reference manipulate the contents of the object it points to (after initialization), not the reference itself (does not change where it points to)</li>\n<li>Arithmetic operations on the reference manipulate the contents of the object it points to, not the reference itself (does not change where it points to)</li>\n<li>In pretty much all implementations, the reference is actually stored as an address in memory of the referred to object.  Hence, it takes up storage in memory equal to the size of the address for the target machine architecture just like a pointer object</li>\n</ul>\n<p>Even though pointers and references are implemented in much the same way &quot;under-the-hood,&quot; the compiler treats them differently, resulting in all the differences described above.</p>\n<h3>Article</h3>\n<p>A recent article I wrote that goes into much greater detail than I can show here and should be very helpful for this question, especially about how things happen in memory:</p>\n<p><a href="https://www.xitalogy.com/programming/2019/08/15/a-tour-of-cpp-arrays-pointers-and-references-under-the-hood.html" rel="nofollow noreferrer">Arrays, Pointers and References Under the Hood In-Depth Article</a></p>\n'}, {'answer_id': 57363123, 'author': 'Gerard ONeill', 'author_id': 1331672, 'author_profile': 'https://Stackoverflow.com/users/1331672', 'pm_score': 1, 'selected': False, 'text': '<p>"I know references are syntactic sugar, so code is easier to read and write"</p>\n\n<p>This.  A reference is not another way to implement a pointer, although it covers a huge pointer use case.  A pointer is a datatype -- an address that in general points to a actual value.  However it can be set to zero, or a couple of places after the address using address arithmetic, etc.  A reference is \'syntactic sugar\' for a variable which has its own value.</p>\n\n<p>C only had pass by value semantics.  Getting the address of the data a variable was referring to and sending that to a function was a way to pass by \'reference\'.  A reference shortcuts this semantically by \'referring\' to the original data location itself.  So:</p>\n\n<pre><code>int x = 1;\nint *y = &amp;x;\nint &amp;z = x;\n</code></pre>\n\n<p>Y is an int pointer, pointing to the location where x is stored.\nX and Z refer to the same storage place (the stack or the heap).</p>\n\n<p>Alot of people have talked about the difference between the two (pointers and references) as if they are the same thing with different usages.  They are not the same at all.  </p>\n\n<p>1) "A pointer can be re-assigned any number of times while a reference cannot be re-assigned after binding." -- a pointer is an address data type which points to data.  A reference is another name for the data.  So you <em>can</em> \'reassign\' a reference.  You just can\'t reassign the data location it refers to.  Just like you can\'t change the data location that \'x\' refers to, you can\'t do that to \'z\'.  </p>\n\n<pre><code>x = 2;\n*y = 2;\nz = 2;\n</code></pre>\n\n<p>The same.  It is a reassignment. </p>\n\n<p>2) "Pointers can point nowhere (NULL), whereas a reference always refers to an object" -- again with the confusion.  The reference is just another name for the object.  A null pointer means (semantically) that it isn\'t referring to anything, whereas the reference was created by saying it was another name for \'x\'.  Since</p>\n\n<p>3) "You can\'t take the address of a reference like you can with pointers" -- Yes you can.  Again with the confusion.  If you are trying to find the address of the pointer that is being used as a reference, that is a problem -- cause references are not pointers to the object.  They are the object.  So you can get the address of the object, and you can get the address of the pointer.  Cause they are both getting the address of data (one the object\'s location in memory, the other the pointer to the objects location in memory).</p>\n\n<pre><code>int *yz = &amp;z; -- legal\nint **yy = &amp;y; -- legal\n\nint *yx = &amp;x; -- legal; notice how this looks like the z example.  x and z are equivalent.\n</code></pre>\n\n<p>4) "There\'s no "reference arithmetic"" -- again with the confusion -- since the example above has z being a reference to x and both are therefore integers, \'reference\' arithmetic means for example adding 1 to the value referred to by x.</p>\n\n<pre><code>x++;\nz++;\n\n*y++;  // what people assume is happening behind the scenes, but isn\'t. it would produce the same results in this example.\n*(y++);  // this one adds to the pointer, and then dereferences it.  It makes sense that a pointer datatype (an address) can be incremented.  Just like an int can be incremented. \n</code></pre>\n'}, {'answer_id': 57728134, 'author': 'Hitesh Jangid', 'author_id': 9473321, 'author_profile': 'https://Stackoverflow.com/users/9473321', 'pm_score': 1, 'selected': False, 'text': '<p>Basic meaning of pointer(*) is "Value at address of" which means whatever address you provide it will give value at that address. Once you change the address it will give the new value, while reference variable used to reference any particular variable and which can\'t be change to reference any other variable in future.   </p>\n'}, {'answer_id': 59591539, 'author': 'S.S. Anne', 'author_id': 10795151, 'author_profile': 'https://Stackoverflow.com/users/10795151', 'pm_score': 3, 'selected': False, 'text': '<p>Summary from answers and links below:</p>\n\n<ol>\n<li>A pointer can be re-assigned any number of times while a reference cannot be re-assigned after binding.</li>\n<li>Pointers can point nowhere (<code>NULL</code>), whereas a reference always refers to an object.</li>\n<li>You can\'t take the address of a reference like you can with pointers.</li>\n<li>There\'s no "reference arithmetic" (but you can take the address of an object pointed by a reference and do pointer arithmetic on it as in <code>&amp;obj + 5</code>).</li>\n</ol>\n\n<p>To clarify a misconception:</p>\n\n<blockquote>\n  <p><em>The C++ standard is very careful to avoid dictating how a compiler may\n  implement references, but every C++ compiler implements\n  references as pointers. That is, a declaration such as:</em></p>\n\n<pre><code>int &amp;ri = i;\n</code></pre>\n  \n  <p><strong><em>if it\'s not optimized away entirely</em></strong>, <em>allocates the same amount of storage\n  as a pointer, and places the address\n  of <code>i</code> into that storage.</em></p>\n</blockquote>\n\n<p><strong><em>So, a pointer and a reference both use the same amount of memory.</em></strong></p>\n\n<p>As a general rule,</p>\n\n<ul>\n<li>Use references in function parameters and return types to provide useful and self-documenting interfaces.</li>\n<li>Use pointers for implementing algorithms and data structures.</li>\n</ul>\n\n<p>Interesting read:</p>\n\n<ul>\n<li>My all-time favorite <a href="https://yosefk.com/c++fqa/ref.html" rel="noreferrer">C++ FAQ lite</a>.</li>\n<li><a href="https://www.embedded.com/references-vs-pointers" rel="noreferrer">References vs. Pointers</a>.</li>\n<li><a href="https://www.embedded.com/an-introduction-to-references" rel="noreferrer">An Introduction to References</a>.</li>\n<li><a href="https://www.embedded.com/references-and-const" rel="noreferrer">References and const</a>.</li>\n</ul>\n'}, {'answer_id': 59748316, 'author': 'Sadhana Singh', 'author_id': 9011579, 'author_profile': 'https://Stackoverflow.com/users/9011579', 'pm_score': 3, 'selected': False, 'text': '<p>in simple words, we can say a reference is an alternative name for a variable whereas,\na pointer is a variable that holds the address of another variable.\ne.g.</p>\n\n<pre><code>int a = 20;\nint &amp;r = a;\nr = 40;  /* now the value of a is changed to 40 */\n\nint b =20;\nint *ptr;\nptr = &amp;b;  /*assigns address of b to ptr not the value */\n</code></pre>\n'}, {'answer_id': 62199065, 'author': 'Lewis Kelsey', 'author_id': 7194773, 'author_profile': 'https://Stackoverflow.com/users/7194773', 'pm_score': 4, 'selected': False, 'text': "<p>A reference is a const pointer. <code>int * const a = &amp;b</code> is the same as <code>int&amp; a = b</code>. This is why there's is no such thing as a const reference, because it is already const, whereas a reference to const is <code>const int * const a</code>. When you compile using -O0, the compiler will place the address of b on the stack in both situations, and as a member of a class, it will also be present in the object on the stack/heap identically to if you had declared a const pointer. With -Ofast, it is free to optimise this out. A const pointer and reference are both optimised away.</p>\n<p>Unlike a const pointer, there is no way to take the address of the reference itself, as it will be interpreted as the address of the variable it references. Because of this, on -Ofast, the const pointer representing the reference (the address of the variable being referenced) will always be optimised off the stack, but if the program absolutely needs the address of an actual const pointer (the address of the pointer itself, not the address it points to) i.e. you print the address of the const pointer, then the const pointer will be placed on the stack so that it has an address.</p>\n<p>Otherwise it is identical i.e. when you print the that address it points to:</p>\n<pre><code>#include &lt;iostream&gt;\n\nint main() {\n  int a =1;\n  int* b = &amp;a;\n  std::cout &lt;&lt; b ;\n}\n\nint main() {\n  int a =1;\n  int&amp; b = a;\n  std::cout &lt;&lt; &amp;b ;\n}\n</code></pre>\n<pre><code>they both have the same assembly output\n-Ofast:\nmain:\n        sub     rsp, 24\n        mov     edi, OFFSET FLAT:_ZSt4cout\n        lea     rsi, [rsp+12]\n        mov     DWORD PTR [rsp+12], 1\n        call    std::basic_ostream&lt;char, std::char_traits&lt;char&gt; &gt;&amp; std::basic_ostream&lt;char, std::char_traits&lt;char&gt; &gt;::_M_insert&lt;void const*&gt;(void const*)\n        xor     eax, eax\n        add     rsp, 24\n        ret\n--------------------------------------------------------------------\n-O0:\nmain:\n        push    rbp\n        mov     rbp, rsp\n        sub     rsp, 16\n        mov     DWORD PTR [rbp-12], 1\n        lea     rax, [rbp-12]\n        mov     QWORD PTR [rbp-8], rax\n        mov     rax, QWORD PTR [rbp-8]\n        mov     rsi, rax\n        mov     edi, OFFSET FLAT:_ZSt4cout\n        call    std::basic_ostream&lt;char, std::char_traits&lt;char&gt; &gt;::operator&lt;&lt;(void const*)\n        mov     eax, 0\n        leave\n        ret\n</code></pre>\n<p>The pointer has been optimised off the stack, and the pointer isn't even dereferenced on -Ofast in both cases, instead it uses a compile time value.</p>\n<p>As members of an object they are identical on -O0 through -Ofast.</p>\n<pre><code>#include &lt;iostream&gt;\nint b=1;\nstruct A {int* i=&amp;b; int&amp; j=b;};\nA a;\nint main() {\n  std::cout &lt;&lt; &amp;a.j &lt;&lt; &amp;a.i;\n}\n\nThe address of b is stored twice in the object. \n\na:\n        .quad   b\n        .quad   b\n</code></pre>\n<pre><code>        mov     rax, QWORD PTR a[rip+8] //&amp;a.j\n        mov     esi, OFFSET FLAT:a //&amp;a.i\n</code></pre>\n<p>When you pass by reference, on -O0, you pass the address of the variable referenced, so it is identical to passing by pointer i.e. the address the const pointer contains. On -Ofast this is optimised out by the compiler in an inline call if the function can be inlined, as the dynamic scope is known, but in the function definition, the parameter is always dereferenced as a pointer (expecting the address of the variable being referenced by the reference) where it may be used by another translation unit and the dynamic  scope is unknown to the compiler, unless of course the function is declared as a static function, then it can't be used outside of the translation unit and then it passes by value so long as it isn't modified in the function by reference, then it will pass the address of the variable being referenced by the reference that you're passing, and on -Ofast this will be passed in a register and kept off of the stack if there are enough volatile registers in the calling convention.</p>\n"}, {'answer_id': 68188598, 'author': 'Sarath Govind', 'author_id': 12284466, 'author_profile': 'https://Stackoverflow.com/users/12284466', 'pm_score': 2, 'selected': False, 'text': '<p>A pointer is a variable that holds the memory address of another variable , where as a reference is an alias to an existing variable. (another name of the already existing variable)</p>\n<p>1.\nA pointer can be initialised as:</p>\n<pre><code>int b = 15;\nint *q = &amp;b;\n</code></pre>\n<p>OR</p>\n<pre><code>int *q;\nq = &amp;b;\n</code></pre>\n<p>where as reference,</p>\n<pre><code>int b=15;\nint &amp;c=b;\n</code></pre>\n<p>(declare and initialize in a single step)</p>\n<ol start="2">\n<li>A pointer can be assigned to null, but reference cannot</li>\n<li>Various arithmetic operations can be performed on pointers whereas there is no such thing called Reference Arithmetic.</li>\n<li>A pointer can be reassigned , but reference cannot</li>\n<li>A pointer has its own memory address and size on the stack whereas a reference shares the same memory address</li>\n</ol>\n'}, {'answer_id': 69281608, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 1, 'selected': False, 'text': '<p>you can not dereference a reference like a pointer, which when dereferenced gives values at that location,</p>\n<p>both reference and pointer work by the address though...</p>\n<p>so</p>\n<p>you can do this</p>\n<p>int* val = 0xDEADBEEF;\n*val is something at 0xDEADBEEF.</p>\n<p>you can not do this\nint&amp; val = 1;</p>\n<p>*val is not allowed.</p>\n'}, {'answer_id': 73017343, 'author': 'CPP_is_no_STANDARD', 'author_id': 18032104, 'author_profile': 'https://Stackoverflow.com/users/18032104', 'pm_score': 0, 'selected': False, 'text': '<p>In short,</p>\n<blockquote>\n<p>Pointers: A pointer is a variable that holds the memory address of another variable. A pointer needs to be dereferenced with the * operator to access the memory location it points to. - Extracted from Geeks for Geeks</p>\n</blockquote>\n<blockquote>\n<p>References: 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. - Extracted from Geeks for Geeks</p>\n</blockquote>\n<p>Another picture for more details:</p>\n<p><a href="https://i.stack.imgur.com/PBfzi.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/PBfzi.png" alt="From the web." /></a></p>\n'}, {'answer_id': 73859509, 'author': 'Ezh', 'author_id': 589119, 'author_profile': 'https://Stackoverflow.com/users/589119', 'pm_score': 2, 'selected': False, 'text': "<p>Think about a pointer as about a business card:</p>\n<ul>\n<li>It gives you a chance to contact someone</li>\n<li>It can be empy</li>\n<li>It can contain wrong or outdated information</li>\n<li>You are not sure someone, mentioned on it, is even still alive</li>\n<li>You can't talk directly to the card, you only can use it to call someone</li>\n<li>Maybe there are many such cards exist</li>\n</ul>\n<p>Think about a reference as about an active call with someone:</p>\n<ul>\n<li>You are pretty sure someone, you contacted, is alive</li>\n<li>You can talk directly, no extra calls are needed</li>\n<li>You are pretty sure you talk not to an empty place or a piece of garbage</li>\n<li>You can't be sure you are the only one, who is currently talking with this object</li>\n</ul>\n"}]
['https://Stackoverflow.com/questions/57483', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/123/']