-
Notifications
You must be signed in to change notification settings - Fork 0
/
passByAddress.c
40 lines (26 loc) · 1.03 KB
/
passByAddress.c
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
#include <stdio.h>
void addTwo(int k);
int main() {
int x = 5;
int *y = &x; // Pointer declaration and initialization
printf("The value of x is: %d\n", x);
printf("The value of x is: %d\n\n", *y);
printf("The address of x is: %p\n", &x);
printf("The address of x is: %p\n\n", y);
addTwo(x); // This is exactly the same as writing addTwo(5)
printf("adding..\n");
printf("The value of x is: %d\n", x);
return 0;
}
void addTwo(int k) { // This creates a new variable k and just gives it the value 5
printf("Getting inside the function.\n");
k += 2; // k = k + 2
printf("The value of k is: %d\n",k);
printf("Getting out of the function and returning to main.\n");
}
void addTwoNew(int *y) { // Reminder: asterisk here is used for pointer declaration
*y = *y + 2; // Reminder: asterisk here is used for pointer dereferencing
}
// Pointer dereferencing
//int z = *y; // equivalent to: int z = 5; (this is pointer dereferencing)
//int *z = y; // equivalent to: int* z = &x; (this is pointer declaration)