-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathswapIntegers-reference.c
More file actions
39 lines (29 loc) · 1.03 KB
/
swapIntegers-reference.c
File metadata and controls
39 lines (29 loc) · 1.03 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
#include <stdio.h>
// Antonio Maulucci 2019
/*
+-------------------+
+---+ | |
a+------>+32 | | +-+tmp+-------+ |
+---+ | | +-----+ | |
0x1 +-----+ | | b| 32 | | |
++swap()| | +-----+ | |
+---+ | | | a+-----+ | |
b+------>+16 |0x2 +--+ | | | 16 <--+ |
+----<-----------------+ +-----+ |
| |
+-------------------+
*/
void swap(int *a, int *b) // change the values inside memory's areas using pointers
{
int tmp = *b; //take value (int) from b pointer
*b = *a; // set the valaue of b pointer to a pointer's value
*a = tmp; // set the a pointer's value to tmp
}
int main()
{
int a=32, b=16;
printf("\na = %d , b = %d\n", a, b);
swap(&a, &b); // pass addresses to function
printf("\na = %d , b = %d\n", a, b);
return 0;
}