-
Notifications
You must be signed in to change notification settings - Fork 0
/
pointer-operations.c
181 lines (144 loc) · 5.15 KB
/
pointer-operations.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/**
* @file pointer-operations.c
* @author kunal jain ([email protected])
* @brief Program to demonstrate various
* pointer operations.
* @version 0.1
* @date 2022-02-20
*
* @copyright Copyright (c) 2022
*
*/
#include<stdio.h>
// Remember ->
// Read * notation as Value at variable *x -> value at x
// and read & notation as Address of variable. &x -> address of x
int main(void) {
int x = 2, y = 5;
char a = 'a';
int z[10];
// At what places in memory are variables stored?
// Use & operator to get address of variable.
printf("At what places in memory are variables stored?\n");
printf("Address of x -> %d\n", &x);
printf("Address of y -> %d\n", &y);
printf("Address of a -> %d\n", &a);
int i;
for (i = 0; i < 10; i++) {
printf("Address of z[%d] -> %d\n", i, &z[i]);
}
// How do you store address of variable?
printf("How do you store address of variable?\n");
// We can not use integer variables to store address
// of variables.
// Illegal. Following code produces warning.
// int l;
// l = &x;
// Use pointer variables to store address of variables.
// Declare integer pointer variable to store
// address of integer variable.
// Declare character pointer variable to store
// address of character variable.
int* ip;
char* cp;
// Use ip variable to store address of x
ip = &x;
cp = &a;
// &x -> Address of x
// ip -> Value of ip is address of x
// This is also called as pointer to int x
printf("Value of integer pointer ip -> %d\n", ip);
printf("Value of character pointer cp -> %d\n", cp);
// What does pointer point to or what is value at pointer ip?
printf("What is value at pointer ip?\n");
// Value at pointer = *(pointer-variable)
// *(ip) -> *(&x) -> Value at address of x -> Value of x
// *ip is same as x.
// * is also called indirection operator and is used to
// dereference a pointer.
printf("Value at ip -> %d\n", *ip);
// How to change value of x using pointers?
printf("Can we change value of x using pointer?\n");
// As *ip is value at address of x, change in *ip
// means change in x.
*ip = 0;
printf("Value of x -> %d\n", x);
// What is a pointer to an array?
printf("What is a pointer to an array?\n");
// Pointer pointing to the first element of an array
// is a pointer to an array
ip = &z[0];
// Another way of writing array pointer
// ap also points to first element of z, as array
// name returns address of first element of array.
int* ap;
ap = z;
// Value of both ip and ap is same.
printf("Value of ip -> %d\n", ip);
printf("Value of ap -> %d\n", ap);
printf("Value of z -> %d\n", z);
// Incrementing pointers
printf("What happens when we increment value of pointer?\n");
ip = &x;
printf("Value of x -> %d\n", x);
printf("Value at ip -> %d\n", *ip);
// Increment one in the "value at" (*) pointer ip
printf("*ip + 1 -> %d\n", *ip + 1);
// Increment value at pointer ip
// Use prefix increment operator for operation
// *ip -> x -> 0
// ++*ip -> ++(*ip) -> *ip = *ip + 1
printf("++*ip -> %d\n", ++*ip);
// Increment in *ip means increment in x.
printf("Value of x -> %d\n", x);
printf("Value at ip -> %d\n", *ip);
printf("Address of x -> %d\n", ip);
// Increment value of pointer ip using
// postfix increment operator
// *ip++ -> *(ip++) -> *(&x++) = *(ip + 1)
// *ip is never incremented,
// only ip is incremented.
printf("*ip++ -> %d\n", *ip++);
// There is no change in value of x. However,
// the memory location that ip points to is now
// incremented and ip is now pointing to memory
// location present after x.
// *ip -> Uninitialized garbage value
// ip -> &x + 4
printf("Value of x -> %d\n", x);
printf("Value at ip -> %d\n", *ip);
printf("Address -> %d\n", ip);
// Increment value of pointer in arrays
printf("Increment value of pointer in an array\n");
// ip points to first element of the
// integer array z
ip = &z[0];
z[0] = 100;
z[1] = 120;
printf("Value of ip -> %d\n", ip);
printf("Value at ip -> %d\n", *ip); // 100
// increment value of pointer
++ip;
// What happens when we increment the pointer?
// When you increment a pointer, the address is
// incremented by the number of bytes used to store
// that type of variable.
// As ip is pointer, ++ip works as
// ip = ip + sizeof(variable)
// After increment ip now points to sequentially next
// integer in the memory.
// In this case ip now points to &z[1].
printf("Value of ip -> %d\n", ip);
printf("Value at ip -> %d\n", *ip); // 120
// ip -> z[0]
// ip + 1 -> z[1]
// ip + 2 -> z[2]
// ...
// ip + m -> z[m]
// This is easily visible in array as when we increment
// pointer pointing to an array, the pointer then points
// to next element in the array
// If we know length of array then we can access all
// elements of array using pointer.
return 0;
}