-
Notifications
You must be signed in to change notification settings - Fork 0
/
Pointer.c
466 lines (394 loc) · 10.4 KB
/
Pointer.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
#include <stdio.h>
#include <conio.h>
// int valuebyusingpointer(int x, int y, int z);
// int pointerdiscussion(int x, int y, int z);
// int fun(int x);
// int basicdeclaration();
// int ProblemOnPointer();
// int twolevelpointer();
// int HardpointerValuChange();
// int easyPointerChange();
// int pointerToArray();
// int ValueToSum();
// int PlayingWithPointerByUsingArray();
// int AddingOneToTheValueByUsingPointer(int *x);
// int AithmeticAdditionUsingPointer();
int AirthmeticSubtractionUsingPointer();
int memoryallocation();
int memoryallocation2();
int rellock();
int relocation();
// int swap(int *n1, int *n2);
int main()
{
int x, y, z;
x = 10;
y = 50;
z = 90;
int *ptr;
ptr = &x;
// basicdeclaration();
// int d= valuechangethroughfunction(y);
// printf("%d",d);
// fun(y);
// printf("%d", y);
// return 0;
// pointerdiscussion(x,y,z);
// valuebyusingpointer(x, y, z);
// printf("%x",d);
// ProblemOnPointer();
// twolevelpointer();
// HardpointerValuChange();
// easyPointerChange();
// pointerToArray();
// ValueToSum();
// PlayingWithPointerByUsingArray();
// printf("The value of first x is %d\n",x);
// printf("The value of first y is %d\n",y);
// swap(&x,&y);
// printf("The value of x is %d\n",x);
// printf("The value of y is %d\n",y);
// AddingOneToTheValueByUsingPointer(ptr);
// printf("%d",x);
// AithmeticAdditionUsingPointer();
// AirthmeticSubtractionUsingPointer();
// memoryallocation();
// memoryallocation2();
// rellock();
// relocation();
// printf("\n");
printf("\nhurray i learnt pointer");
}
int pointerdiscussion(int x, int y, int z)
{
int *ptr1, *ptr2, *ptr3;
// int *ptr2;
// int *ptr3;
ptr1 = &x;
ptr2 = &y;
ptr3 = &z;
printf("%x\n%x\n%x", ptr1, ptr2, ptr3);
}
int valuebyusingpointer(int x, int y, int z)
{
int *ptr1, *ptr2, *ptr3;
ptr1 = &x;
ptr2 = &y;
ptr3 = &z;
printf("\nThe address of pointer 1 is %x", ptr1);
printf("\nThe value of first pointer is %d", *ptr1);
printf("\nThe address of pointer 2 is %x", ptr2);
printf("\nThe value of first pointer is %d", *ptr2);
printf("\nThe address for pointer 3 is %x", ptr3);
printf("\nThe value of first pointer is %d", *ptr3);
// printf("");
// printf("The value of first pointer is %d",*ptr1);
}
int fun(int x)
{
// x = 30;
int *ptr = &x;
}
// Write a program in C to show the basic declaration of a pointer Expected Output :Pointer : Show the basic declaration of pointer:Here is m=10, n and o are two integer variable and *z is an nteger z stores the address of m = 0x7ffd40630d44 *z stores the value of m = 10 &m is the address of m =x7ffd40630d44 &n stores the address of n = 0x7ffd40630d48 &o stores the address of o =0x7ffd40630d4c &z stores the address of z= 0x7ffd40630d50
int basicdeclaration()
{
int m = 1000;
int n, o;
int *z;
z = &m;
printf("\nthe value of m is %d", m);
printf("\nThe value of m through address is %x", *z);
printf("\nThe adress of n is %x", &n);
printf("\nThe adress of o is %x", &o);
}
int valuechangethroughfunction(int x)
{
x = 400;
return 0;
}
// some basic problems on pointer
int ProblemOnPointer()
{
int x = 10, y = 18;
int *ptr1, *ptr2;
ptr1 = &x;
// ptr2 = &y;
ptr2 = &y;
*ptr2 = *ptr1;
printf("a = %d %d %d", x, *ptr1, *ptr2);
}
// Two levelpointer
int twolevelpointer()
{
int s = 12111230;
int *ptr1, **ptr2;
ptr1 = &s;
ptr2 = &ptr1;
printf("The address of s is %x", &s);
printf("\n");
printf("The value of s is %d", s);
printf("\n");
printf("The address of ptr1 is %x", ptr1);
printf("\n");
printf("The value of ptr1 is %d", *ptr1);
printf("\n");
printf("The address of ptr2 is %x", ptr2);
printf("\n");
printf("The value of ptr2 is %p", **ptr2);
}
int HardpointerValuChange()
{
int *pc, c;
c = 22;
printf("Address of c: %p\n", &c);
printf("Value of c: %d\n\n", c);
pc = &c;
printf("Address of pointer pc: %p\n", pc);
printf("Content of pointer pc: %d\n\n", *pc);
c = 11;
printf("Address of pointer pc: %p\n", pc);
printf("Content of pointer pc: %d\n\n", *pc);
*pc = 2;
printf("Address of c: %p\n", &c);
printf("Value of c: %d\n\n", c); // 2
return 0;
}
int easyPointerChange()
{
int *pc, c;
c = 5;
pc = &c;
*pc = 1;
printf("%d\n", *pc);
printf("%d", c);
}
// Pointer To Array
// a program where we got to know the base adress of the array is the array first element address aactually
int pointerToArray()
{
int arr[5];
int len = sizeof(arr) / sizeof(arr[0]);
// printf("lenggth is %d",len);
for (int i = 0; i < len; i++)
{
printf("%x\n", &arr[i]);
}
printf("%x", &arr);
printf("\n");
printf("%x", &arr[1]);
printf("\n");
printf("%x", &arr[2]);
printf("\n");
printf("%x", &arr[3]);
printf("\n");
printf("%x", &arr[4]);
}
// By taking value from adress and printing the sum of an array
int ValueToSum()
{
int arr[5], sum = 0;
int len = sizeof(arr) / sizeof(arr[0]);
for (int i = 0; i < len; i++)
{
scanf("%d", &arr[i]);
sum += *(arr + i);
printf("\n");
printf("%x", *(arr + i));
printf("\n");
}
printf("%d", sum);
}
// lets play with the pointers by using array
int PlayingWithPointerByUsingArray()
{
int arr[5] = {10, 20, 30, 40, 50};
int *p = arr;
int *p2 = &arr[0];
int *ptr1 = &arr[2];
int *ptr3;
printf("Ptr2 is %x\n = ", p2);
printf("Ptr1 is %x\n = ", p);
printf("Ptr2 is %x\n = ", ptr1);
int len = sizeof(arr) / sizeof(arr[0]);
for (int i = 0; i < len; i++)
{
ptr3 = &arr[i];
printf("Ptr%d is %x\n = ", i + 1, ptr3);
}
}
// Important Topic
// Pointer to function
int swap(int *n1, int *n2)
{
int temp;
temp = *n2;
*n2 = *n1;
*n1 = temp;
}
// Adding one to the pointer by using function
int AddingOneToTheValueByUsingPointer(int *x)
{
// *(x)++;
++(*x);
}
// Airthmetic Pointer
// ================>>>>>>>>>>>>>>>>>>> Addition Problems Based on array and Integers
int AithmeticAdditionUsingPointer()
{
int a[5] = {0, 1, -1, 10, 11};
int *p = &a[0];
printf("%d\n", *p);
printf("the adress is %p\n", p);
p = p + 2;
printf("%d\n", *p);
printf("the adress is %p\n", p);
p = p + 1;
*p = 2;
printf("%d", *p);
printf("the adress is %p", p);
}
// ==================>>>>>>>>>>>>>>> Subtraction Problem Based on array and Integers
// We can subtract pointer with another pointer and it is going to give you some kind of an integer value for example (pinter - pointer) while we can also subtract it from an integervalue for example (pointer-2) it is going to give you the Pointer value like an adress of some variable or a memory adress based on your inputs
int AirthmeticSubtractionUsingPointer()
{
printf("We are going to learn airthmetic subtraction of pointer");
}
int memoryallocation()
{
int n, i, *ptr, sum = 0;
printf("Enter number of elements: ");
scanf("%d", &n);
ptr = (int *)malloc(n * sizeof(int));
if (ptr == NULL)
{
printf("Error! memory not allocated.");
exit(0);
}
printf("Enter elements: ");
for (i = 0; i < n; ++i)
{
scanf("%d", ptr + i);
sum += *(ptr + i);
}
printf("Sum = %d", sum);
free(ptr);
return 0;
}
int memoryallocation2()
{
int n, i, *ptr, sum = 0;
printf("Enter number of elements: ");
scanf("%d", &n);
ptr = (int *)calloc(n, sizeof(int));
if (ptr == NULL)
{
printf("Error! memory not allocated.");
exit(0);
}
printf("Enter elements: ");
for (i = 0; i < n; ++i)
{
scanf("%d", ptr + i);
sum += *(ptr + i);
}
printf("Sum = %d", sum);
free(ptr);
return 0;
}
int rellock()
{
int *ptr, i, n1, n2;
printf("Enter size: ");
scanf("%d", &n1);
ptr = (int *)malloc(n1 * sizeof(int));
printf("Addresses of previously allocated memory:\n");
for (i = 0; i < n1; ++i)
printf("%pc\n", ptr + i);
printf("\nEnter the new size: ");
scanf("%d", &n2);
ptr = realloc(ptr, n2 * sizeof(int));
printf("Addresses of newly allocated memory:\n");
for (i = 0; i < n2; ++i)
printf("%pc\n", ptr + i);
free(ptr);
return 0;
}
int rellock()
{
int *ptr, i, n1, n2;
printf("Enter size: ");
scanf("%d", &n1);
ptr = (int *)malloc(n1 * sizeof(int));
printf("Addresses of previously allocated memory:\n");
for (i = 0; i < n1; ++i)
printf("%pc\n", ptr + i);
printf("\nEnter the new size: ");
scanf("%d", &n2);
ptr = realloc(ptr, n2 * sizeof(int));
printf("Addresses of newly allocated memory:\n");
for (i = 0; i < n2; ++i)
printf("%pc\n", ptr + i);
free(ptr);
return 0;
}
int relocation()
{
int n;
double *data;
printf("Enter the total number of elements: ");
scanf("%d", &n);
data = (double *)calloc(n, sizeof(double));
if (data == NULL)
{
printf("Error!!! memory not allocated.");
exit(0);
}
for (int i = 0; i < n; ++i)
{
printf("Enter number%d: ", i + 1);
scanf("%lf", data + i);
}
for (int i = 1; i < n; ++i)
{
if (*data < *(data + i))
{
*data = *(data + i);
}
}
printf("Largest number = %.2lf", *data);
free(data);
return 0;
}
// Revising the code as it is not my code i will try to make it more simple
int addingmatrix() {
int r, c, a[100][100], b[100][100], sum[100][100], i, j;
printf("Enter the number of rows (between 1 and 100): ");
scanf("%d", &r);
printf("Enter the number of columns (between 1 and 100): ");
scanf("%d", &c);
printf("\nEnter elements of 1st matrix:\n");
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
printf("Enter element a%d%d: ", i + 1, j + 1);
scanf("%d", &a[i][j]);
}
printf("Enter elements of 2nd matrix:\n");
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
printf("Enter element b%d%d: ", i + 1, j + 1);
scanf("%d", &b[i][j]);
}
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
sum[i][j] = a[i][j] + b[i][j];
}
printf("\nSum of two matrices: \n");
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
printf("%d ", sum[i][j]);
if (j == c - 1) {
printf("\n\n");
}
}
return 0;
}