-
Notifications
You must be signed in to change notification settings - Fork 0
/
DMA.c
81 lines (76 loc) · 1.73 KB
/
DMA.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
#include<stdio.h>
#include<stdlib.h>
void main(){
//malloc for memory allocation
//calloc for continuos allocation
//free()
//realloc for reallocation
//malloc
int *ptr;
ptr= (int *) malloc(5 * sizeof(int));
ptr[0]=1;
ptr[1]=3;
ptr[2]=5;
ptr[3]=7;
ptr[4]=9;
for(int i=0;i<5;i++){
printf("%d\n",ptr[i]);
}
float *ptr1;
ptr1=(float*) malloc(5*sizeof(float));
ptr1[0]=1;
ptr1[1]=4;
ptr1[2]=6;
ptr1[3]=8;
ptr1[4]=9;
for(int i=0;i<5;i++){
printf("%f\n",ptr1[i]);
}
// calloc function always initializes with zero
int *ptr2;
ptr2=(int *) calloc(5,sizeof(int));
for(int i=0;i<5;i++){
printf("%d\n",ptr2[i]);
}
int n;
int *ptr3;
printf("Enter n :");
scanf("%d",&n);
ptr3=(int *)calloc(n,sizeof(int));
for(int i=0;i<n;i++){
printf("%d\n",ptr3[i]);
}
//free() we use it to free memory allocated by calloc and malloc
free(ptr3);
ptr3=(int *)calloc(3,sizeof(int));
for(int i=0;i<3;i++){
printf("%d\n",ptr3[i]);
}
// realloc :increase or decrease memory using the same pointer & size
int *ptr4;
ptr4=(int*)calloc(5,sizeof(int));
printf("Enter num (5):");
for(int i=0;i<5;i++){
scanf("%d",&ptr4[i]);
}
ptr4=realloc(ptr4,8);
printf("Enter num (8):");
for(int i=0;i<8;i++){
scanf("%d",&ptr4[i]);
}
//print
for(int i=0;i<8;i++){
printf("Number %d is %d\n",i,ptr4[i]);
}
int *ptr5;
ptr5=(int*)calloc(5,sizeof(int));
printf("enter first Five odd numbers :");
for(int i=0;i<5;i++){
scanf("%d",&ptr5[i]);
}
ptr5=realloc(ptr5,6);
printf("Enter First 6 Even Numbers :");
for(int i=0;i<5;i++){
scanf("%d",&ptr5[i]);
}
}