-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNumSwapUsingPointers.cpp
More file actions
56 lines (45 loc) · 1.1 KB
/
Copy pathNumSwapUsingPointers.cpp
File metadata and controls
56 lines (45 loc) · 1.1 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
54
55
56
#include <iostream>
using namespace std;
void swapvalue(int *a, int *b)
{
int temp;
temp = *a;
*a = *b;
*b = temp;
}
int main()
{
int a, b;
cout << "Enter two numbers: ";
cin >> a >> b;
swapvalue(&a, &b);
cout << "After swapping: first number = " << a
<< ", second number = " << b << endl;
return 0;
}
/*
Output will be:
____________________
Sample Input:
10 20
Sample Output:
After swapping: first number = 20, second number = 10
_____________________
Concept:
________________________
This program demonstrates Call by Pointer in C++.
The addresses of variables are passed to the function.
Working:
1. &a and &b send the addresses of variables.
2. Inside function, *a and *b access actual values.
3. Values are swapped using a temporary variable.
____________________________________
Advantages:
1. Original values can be modified.
2. Efficient because no copying of variables.
3. Useful in arrays and dynamic memory handling.
Disadvantages:
1. More complex syntax than reference.
2. Risk of null or invalid pointer errors.
3. Harder to debug in large programs.
*/