-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpointers_n_refrerences.cpp
More file actions
50 lines (37 loc) · 1.05 KB
/
pointers_n_refrerences.cpp
File metadata and controls
50 lines (37 loc) · 1.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// C++ program to demonstrate differences between pointer
// and reference.
// from https://www.geeksforgeeks.org/passing-by-pointer-vs-passing-by-reference-in-c/
#include <iostream>
#include <stdio.h>
using namespace std;
struct demo
{
int a;
};
int main()
{
int x = 5;
int y = 6;
cout << "pointers and refs!" << x << endl;
demo d;
int *p;
p = &x;
int &r = x;
// &r = y; // Compile Error
r = y; // x value becomes 6
// p = NULL;
// &r = NULL; // Compile Error
p++; // Points to next memory location
r++; // x values becomes 7
cout << "address of p " << &p << " address of x " << &x << endl; // Different address
cout << "address of r " << &r << " value of x " << x << endl; // Same address
demo *q = &d;
demo &qq = d;
q->a = 8;
// q.a = 8; // 5. Compile Error
qq.a = 8;
// qq->a = 8; // 5. Compile Error
cout << "value of p " << p << endl; // 6. Prints the address
cout << "value of r " << r << endl; // 6. Print the value of x
return 0;
}