-
Notifications
You must be signed in to change notification settings - Fork 0
/
L11_Pointers.c
99 lines (77 loc) · 1.55 KB
/
L11_Pointers.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
void pointerDemo()
{
int a = 1;
int b = 2;
int *ptr = &a;
printf("%d\n",ptr);
ptr = &b;
printf("%d\n",ptr);
printf("%d\n",*ptr);
//+,-,<,>
printf("%d\n",*ptr);
ptr+=1;
printf("%d",*ptr);
ptr-=1;
printf("%d\n",*ptr);
printf("%d\n",ptr>(&a));
}
void pointerArray()
{
char *p;
char str[] = "Hello world";
p = str;
while(*p)
{
printf("%c",*p);
p+=1;
}
int a,b,c,d;
int *ptrArray[10];
ptrArray[0] = &a;
ptrArray[1] = &b;
ptrArray[2] = &c;
ptrArray[3] = &d;
printf("%d\n",ptrArray[0]);
printf("%d\n",ptrArray[1]);
printf("%d\n",ptrArray[2]);
}
void add(int a,int b)
{
printf("%d",a+b);
}
void subtract(int a,int b)
{
printf("%d",a-b);
}
void performOperation(void (*functionPointer) (const int,const int))
{
functionPointer(10,30);
}
void pointerToFunction()
{
void (*functionPointer) (const int,const int);
functionPointer = add;
performOperation(functionPointer);
}
void dyanamicAllocatedArray()
{
int n = 5;
int *ptr = (int*)malloc(n * sizeof(int));
int *ptr2 = (int*)calloc(n,sizeof(int));
// Memory has been successfully allocated
printf("Memory successfully allocated using malloc.\n");
// Get the elements of the array
int i=0;
for (i = 0; i < n; ++i) {
ptr[i] = i + 1;
}
printf("The elements of the array are: ");
for (i = 0; i < n; ++i) {
printf("%d, ", ptr[i]);
}
//ptr2 = (int*)realloc(ptr,n*sizeof(int));
for (i = 0; i < n; ++i) {
printf("%d, ", ptr2[i]);
}
free(ptr);
}