-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpointers_with_arrays.c
71 lines (50 loc) · 1.65 KB
/
pointers_with_arrays.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
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
// Program to understand how pointers and multi dimensional arrays work
#include <stdio.h>
#ifdef ADDRESS
#undef ADDRESS
#else
#define VALUE
#endif
void two_dimensional_array(void){
/*
2 1D array of 5 elements each
1D array of 5 elements is of 20 bytes + 1D array of 5 elements is of 20 bytes = 40 bytes
*/
int arr[2][5] = { {1,2,3,4,5},
{7,8,9,10,11}
};
// int *p = arr /* wrong */
int (*p) [5] = arr;
printf("val: %p at line: %d\n", *arr,__LINE__);
printf("val: %p at line: %d\n", arr,__LINE__);
printf("val: %p at line: %d\n", arr+1, __LINE__);
printf("val: %p at line: %d\n", *(arr+1), __LINE__);
printf("val: %p at line: %d\n", *(arr+1) +2, __LINE__);
printf("val: %d at line: %d\n", *(*(arr+1)+2),__LINE__);
}
void one_dimensional_array(void) {
/* Declaring an one-dimensional array */
int arr[5] = {1,2,3,4,5}; /* One-dimensional array of 5 elements */
int *ptr = arr; /*arr returns pointer to the first element in array */
#ifdef ADDRESS
for(int i=0 ; i<5; i++)
printf("val: %p at line %d\n",&arr[i],__LINE__);
printf("val: %p at line: %d\n",arr,__LINE__);
printf("val: %p at line: %d\n",arr+1,__LINE__);
printf("val: %p at line: %d\n",arr+2,__LINE__);
printf("val: %p at line: %d\n",arr+3,__LINE__);
printf("val: %p at line: %d\n",arr+4,__LINE__);
#endif
#ifdef VALUE
printf("val: %d at line: %d\n",*ptr,__LINE__);
printf("val: %d at line: %d\n", *(ptr+1),__LINE__);
printf("val: %d at line: %d\n", *(ptr+2),__LINE__);
printf("val: %d at line: %d\n", *(ptr+3),__LINE__);
printf("val: %d at line: %d\n", *(ptr+4),__LINE__);
#endif
}
int main(void) {
one_dimensional_array();
two_dimensional_array();
return 0;
}