difference between pointer and reference in c 2b 2b

Solutions on MaxInterview for difference between pointer and reference in c 2b 2b by the best coders in the world

showing results for - "difference between pointer and reference in c 2b 2b"
Salvatore
08 Feb 2018
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.
Andres
12 Jun 2016
1#include<iostream>
2
3/*
4Pointers: *ptr, point to the memory location of a variable
5int a = 10
6int *ptr = &a //points to the location in memory (0x80ea or whatever) 
7instead of the value
8
9in order for pointers to work, the variable it's pointing to needs to 
10be de-referenced using &.(If confused, remember that the variable, int a, 
11is itself a reference to the location of the value you set it to).
12
13A reference variable: &ref, points to another variable.
14
15int b = 20;
16int &ref = b // points to the value of b, which is 20.
17
18run this if confused:
19*/
20
21    int a = 10;
22    int *ptr = &a;
23    std::cout << "int a value: " << a << std::endl;
24    std::cout << "int ptr value: " << ptr << std::endl;
25
26    int b = 20;
27    int& ref = b;
28    std::cout << "int b value: " << b << std::endl;
29    std::cout << "int ref value: " << ref << std::endl;
30
31    ref = a;
32    std::cout << "int ref after setting it equal to a: " << ref << std::endl;
33    ref = *ptr;
34    std::cout << "int ref after setting it equal to *ptr: " << ref << std::endl;
35    ptr = &ref;
36    std::cout << "ptr after setting it equal to &ref: " << ptr << std::endl; 
37    ptr = &b;
38    std::cout << "ptr after setting it equal to &b: " << ptr << std::endl;
39
40/*
41Reference variables CANNOT be set to a pointer variable; In the case above, you 
42see we can't just put ref = ptr; ptr HAS to be dereferenced with a *, which in 
43turn will give us the value of a, or 10. (dereference pointers with *)
44
45Same goes for pointer variables being set to a reference; you have to dereference 
46the reference value (ptr = &b instead of ptr = b;). In the block above, when we 
47set ptr = &ref, the ref variable is dereferenced showing us a memory location. 
48When ptr=&b is called and we see the output, we noticed it is the same as the previous 
49output.
50*/
Agustín
04 Nov 2019
1//Passing by Pointer://
2
3// C++ program to swap two numbers using 
4// pass by pointer. 
5#include <iostream> 
6using namespace std; 
7  
8void swap(int* x, int* y) 
9{ 
10    int z = *x; 
11    *x = *y; 
12    *y = z; 
13} 
14  
15int main() 
16{ 
17    int a = 45, b = 35; 
18    cout << "Before Swap\n"; 
19    cout << "a = " << a << " b = " << b << "\n"; 
20  
21    swap(&a, &b); 
22  
23    cout << "After Swap with pass by pointer\n"; 
24    cout << "a = " << a << " b = " << b << "\n"; 
25} 
26o/p:
27Before Swap
28a = 45 b = 35
29After Swap with pass by pointer
30a = 35 b = 45
31
32//Passing by Reference://
33
34// C++ program to swap two numbers using 
35// pass by reference. 
36  
37#include <iostream> 
38using namespace std; 
39void swap(int& x, int& y) 
40{ 
41    int z = x; 
42    x = y; 
43    y = z; 
44} 
45  
46int main() 
47{ 
48    int a = 45, b = 35; 
49    cout << "Before Swap\n"; 
50    cout << "a = " << a << " b = " << b << "\n"; 
51  
52    swap(a, b); 
53  
54    cout << "After Swap with pass by reference\n"; 
55    cout << "a = " << a << " b = " << b << "\n"; 
56} 
57o/p:
58Before Swap
59a = 45 b = 35
60After Swap with pass by reference
61a = 35 b = 45
62
63//Difference in Reference variable and pointer variable//
64
65References are generally implemented using pointers. A reference is same object, just with a different name and reference must refer to an object. Since references can’t be NULL, they are safer to use.
66
67A pointer can be re-assigned while reference cannot, and must be assigned at initialization only.
68Pointer can be assigned NULL directly, whereas reference cannot.
69Pointers can iterate over an array, we can use ++ to go to the next item that a pointer is pointing to.
70A pointer is a variable that holds a memory address. A reference has the same memory address as the item it references.
71A pointer to a class/struct uses ‘->'(arrow operator) to access it’s members whereas a reference uses a ‘.'(dot operator)
72A pointer needs to be dereferenced with * to access the memory location it points to, whereas a reference can be used directly.
Alice
11 Apr 2020
1Pointers: 
2A pointer is a variable that holds memory address of another variable. 
3A pointer needs to be dereferenced with * operator to access the 
4memory location it points to. 
5
6References :
7 A reference variable is an alias, that is, 
8another name for an already existing variable.
9 A reference, like a pointer, is also implemented 
10by storing the address of an object. 
queries leading to this page
pointers and references c 2b 2breferences and pointers c 2b 2bwhen to use pointers vs references c 2b 2breference vs pointer in c4 29 what is the difference between reference and pointer 3fc 2b 2b why is 26 used for both references and pointersc 2b 2b difference between pointer and referencethe difference between pointer and referencedifference between call by reference and call by pointerwhy references are different from pointersc 2b 2b pointer vs thisc 2b 2b 26 vs 2apointer vs pointer referenceprogramming language where you must reference pointerswhat is difference between reference and pointerwhat would be the difference between a pointer and a reference 3frefrence and pointer cppc 2b 2b pointer and referencewhen use reference vs pointer object c 2b 2bpointers vs references in cpointer refences cppc pointers 3e vs difference between pointer and reference in cdifferences between pointer and reference in cwhy would you use a pointer vs a aliasdiff between pointer and reference in cdifference between reference and pointerdifference between passing a pointer and a referencereferences in cpp vs cdifference in pointer and referencereferneces vs pointersdifference between reference and pointers in cpointers and reference in c 2b 2bdifference of two pointers in c 2b 2bc pointed vs referencedifference between reference and pointer in cwhat is the difference between reference and pointer 3fdifference of pointer and referencepointer and reference differenceis there any difference between reference variable and pointerdifference between reference and pointer in c 2b 2bc 2b 2b why use pointers vs regular variableshow are references and pointers the samedifference between value of pointer and pointer referencec 2b 2b pointer differencereferences vs pointers in c 2b 2bcpp references and pointersdiff between pointer and referencesreferences and pointers in c 2b 2bc 2b 2b reference variable vs pointerwhat is the difference between reference and pointer in c 2b 2bpointers vs reference c 2b 2bc pointer vs referencediff between pointer and referencepointer and reference difference in c 2b 2bwhat is the difference between pointer and reference typewhat is the difference between a reference and a pointer 3f explain with an examplehow if ponter different form referencec 2b 2b function pointer vs referencepointer vs reference c 2b 2bpointer versus referencepointers and references in cppc 2b 2b difference reference pointer to functinreference vs pointers c 2b 2bc and c 2b 2b both support pointer and references reference vs dereference pointer c 2b 2bc 2b 2b pointers and references explainedpointer reference in c 2b 2bdiffernce between reference and poitnercpp pointers and referencesdfference between references and pointerspointer vs reference cdifference between pointer and refrencewhat is the difference between pointers and references in c 2b 2bdifference between reference variable and a pointer variablehe difference between a pointer variable and reference variable pointers and references in c 2b 2bdifference reference and pointer c 2b 2breference vs pointedifference between referneces and pointersc 2b 2b reference vs pointerdifference pointer and referencedifference in reference variable and pointer variablewhat is the difference between a pointer and a reference 3fwhen to use pointers cppdifference between references and pointerswhy would you use a pointer vs a alias c 2b 2bcpp pointer vs referencepointers and referencesdifference between pointers and references 26 in c 2b 2bdifference between and 3e in pointerswhat is the differece between reference and pointerwhat are the differences between references and pointers 3fdifference between reference variable and pointerc 2b 2b difference between reference and pointercpp reference vs pointerpointers vs references in c 2b 2b forumthe difference between pointers and references is thatc 2b 2b difference between pointer in classwhat is difference between reference variable and pointer in c 2b 2bc 2b 2b diffferance between pointers and referancewhat is the difference between reference and pointerc 2b 2b difference between a pointer and a referencedifference between where you put pointer astreix c 2b 2bwhen and why use pointers and references in c 2b 2bdifference between a pointer and a reference c 2b 2bdifference between ref and pointerwhen to use reference vs pointer c 2b 2bdifference between pointer and reference c 2b 2bc 2b 2b pointer or referencewhat is defrence between pointer and reference difference between passing a variable as a 28pointer and a reference in c 2b 2breference vs pointer in c 2b 2bthe deference between pointer and referencepointer vs reference cppare pointers and references the same thingc 2b 2b when to use reference vs pointerdifference between ref and pointer pythonreferences vs pointerspointers vs referenceswhat are pointers used for in c 2b 2b vs referencereference vs pointer cpptrue or false reference variables are similar to pointer variables 2c but there are some important differences what is the difference between a pointer and a reference c 2b 2bwhen to use a pointer vs referencewhat is the difference between a pointer and a referencedifference between pointers and addresses in c 2b 2bc 2b 2b pointer vs referencepointer vs reference variable cdifference between pointer 2a and pointer 2areference vs pointerc 2b 2b by reference vs pointerdifference between refernce and pointerc 2b 2b difference reference pointer to functionreference vs pointer c 2b 2bc 2b 2b address vs referencereference to a pointer importancedifference between pointer and reference reference vs pointer differncereference and pointer differencedifference between pointer variable and reference variablec 2b 2b reference to pointer variablec pointer 2a vs 2a 2awhat is a pointer 3f difference between pointer and reference differences between references and pointersreference and pointers cdifference between pointers and references in c 2b 2bare pointers and references the same in c 2b 2bc 2b 2b pointers vs referencesi vs pointer i in cpointer reference vs referencedifference between pointer declarationpointers vs references in c 2b 2bc 2b 2b pointers and referencesc difference between pointer and referencereference and pointer c 2b 2breferences vs pointers c 2b 2breference and pointers differencediff between pointer and reference in c 2b 2breference pointerc 2b 2b pointers and reference differencepointer vs reference in c 2b 2bc 2b 2b use pointers or referencesc 2b 2b references and pointerswhat is the difference bw reference and pointerreference type vs pointer c 2b 2breference vs pointersdifference between reference and pointers in c 2b 2bthe difference between pointer and reference in c 2b 2bdifference between a pointer and a referencedifference reference and pointerdifference between a reference and a pointerdifference between reference and pointer c 2b 2bc 2b 2b when to use references and whe to use pointerswhat are the differences between a pointer variable and a reference variable in c 2b 2b 3fdifference between references and pointers in c 2b 2bpointer vs referencedifference between passing reference and pointerdifference between pointer and reference in c 2b 2breference vs dereference pointers c 2b 2bdifference between pointer and referencereference pointer differencec reference vs pointerwhen should i use pointers in c 2b 2bwhen should i use pointer vs reference c 2b 2baddress vs reference c 2b 2breference c 2b 2b vs pointercpp reference vs pointer which is betterpointer vs reference graphicc 2b 2b pointers and referencespointer reference c 2b 2bcpp objects vs pointersdifference between pointer and reference in c 2b 2b