-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpointer_usage.cpp
More file actions
53 lines (41 loc) · 1.08 KB
/
pointer_usage.cpp
File metadata and controls
53 lines (41 loc) · 1.08 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
51
52
53
#include<iostream>
using namespace std;
// Function 1: Adds two integers using pointers
// It takes addresses of two integers as parameters
int add(int*a,int*b){
int sum = *a+*b;
return sum;
}
// Function 2: Adds two integers using normal parameters (by value)
// This is function overloading (same function name, different parameters)
int add(int a, int b){
int sum = a + b;
return sum;
}
void changeB(int* b){
*b = 10;
return;
}
void changeB(int b){
b = 20;
return;
}
int main(){
int a = 10;
int b = 22;
// Calling pointer version of add()
// &a and &b pass the addresses of variables
int d = add(&a,&b);
cout << "Pointer version of add : " << d << endl;
// Calling normal version of add()
// a and b pass the actual values
d = add(a,b);
cout << "Normal version of add : " << d << endl;
cout << endl;
cout << "Initial B : " << b << endl;
changeB(&b);
cout << "After Pointer version of changeB : " << b << endl;
changeB(b);
cout << "After Normal version of changeB : " << b << endl;
return 0;
}