pointers vs references in c 2b 2b

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

showing results for - "pointers vs references in c 2b 2b"
Florencia
07 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*/
Emiliano
04 May 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
pointer vs reference c 2b 2bc 2b 2b difference reference pointer to functincpp references and pointersdifference between pointer and reference in c 2b 2breferences vs pointers in c 2b 2breference vs pointer c 2b 2breference vs dereference pointers c 2b 2bpointer and reference difference in c 2b 2bc 2b 2b difference between reference and pointerwhat is the difference between a pointer and a reference c 2b 2bcpp pointer vs referencec 2b 2b diffferance between pointers and referancec 2b 2b reference vs pointerc 2b 2b difference between pointer in classreferences vs pointers c 2b 2bdifference between passing a variable as a 28pointer and a reference in c 2b 2bdifference between a pointer and a reference c 2b 2breference vs pointer cppdifference of two pointers in c 2b 2bcpp reference vs pointerpointer vs reference cppc 2b 2b pointers and reference differencepointers vs references in ccpp reference vs pointer which is betterc 2b 2b function pointer vs referencewhat is the difference between pointers and references in c 2b 2bdifference between pointer and reference c 2b 2bdifference between reference and pointer c 2b 2bpointers vs references in c 2b 2b forumwhen to use reference vs pointer c 2b 2bwhat are pointers used for in c 2b 2b vs referencec 2b 2b pointer differencereference vs pointers c 2b 2bcpp pointers and referenceswhen should i use pointer vs reference c 2b 2bdiff between pointer and reference in c 2b 2bdifference between where you put pointer astreix c 2b 2bdifference between pointers and references in c 2b 2bdifference between reference and pointer in c 2b 2brefrence and pointer cppc 2b 2b when to use reference vs pointerc 2b 2b pointer vs referencereference vs pointer in c 2b 2bc 2b 2b pointers and referencesdifference between references and pointers in c 2b 2bpointers vs referencesc 2b 2b difference reference pointer to functionpointers vs references in c 2b 2bc 2b 2b pointers vs referenceswhat is difference between reference variable and pointer in c 2b 2breference vs dereference pointer c 2b 2breference c 2b 2b vs pointerreference type vs pointer c 2b 2bdifference between reference and pointers in c 2b 2bpointers vs references in c 2b 2b