-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path29_pointers.c
More file actions
70 lines (51 loc) · 1.45 KB
/
29_pointers.c
File metadata and controls
70 lines (51 loc) · 1.45 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <stdio.h>
int main()
{
// int myAge = 43; // An int variable
// int* ptr = &myAge; // A pointer variable, with the name ptr, that stores the address of myAge
//
// // Output the value of myAge (43)
// printf("%d\n", myAge);
//
// // Output the memory address of myAge (0x7ffe5367e044)
// printf("%p\n", &myAge);
//
// // Output the memory address of myAge with the pointer (0x7ffe5367e044)
// printf("%p\n", ptr);
// printf(" ----- you can also use pointers to access arrays.\n");
//
int nums[] = {5, 7, 6, 8, 4, 2, 9};
// int i;
//
// for(i = 0; i < 6; i++){
// printf("value: %d , memory_address: %p\n", nums[i], &nums[i]);
// }
// int cnt = 56;
// printf("%lu\n", sizeof(cnt));
printf(" ----- How Are Pointers Related to Arrays \n");
// Get the value of the first element in myNumbers
// printf("%d", *nums);
int i;
// way one
// for(i = 0; i < 6; i++){
// printf("value: %d , memory_address: %p\n", nums[i], *nums + i );
// }
// way two accesss values number array
int *ptr = nums;
printf("before change array values with pointer\n");
for(i = 0; i < 6; i++){
printf("value: %d , memory_address: %p\n", nums[i], *ptr + i );
}
*(nums) = 15;
*(nums + 1) = 17;
*(nums + 2) = 16;
*(nums + 3) = 18;
*(nums + 4) = 14;
*(nums + 5) = 12;
*(nums + 6) = 19;
printf("\nAfter change array values with pointer\n");
for(i = 0; i < 6; i++){
printf("value: %d , memory_address: %p\n", nums[i], *ptr + i );
}
return 0;
}