-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDYNAMIC MEMORY ALLOCATION -
57 lines (56 loc) · 1.01 KB
/
DYNAMIC MEMORY ALLOCATION -
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
#include <stdio.h>
#include <stdlib.h>
int main()
{
int choice;
printf("\nMenu:\n");
printf("1. Enter elements\n");
printf("2. Reallocate memory\n");
printf("3. Print elements\n");
printf("4. End\n");
while(1)
{
printf("Enter your choice (1-4): ");
scanf("%d",&choice);
int n,n1,*ptr,*ptr1;
if(choice==1) {
printf("Enter the size: ");
scanf("%d",&n);
ptr=(int *)malloc(n*sizeof(int));
printf("Enter the elements: ");
for(int i=0;i<n;i++)
{
scanf("%d",&ptr[i]);
}
// continue;
}
else if(choice==2) {
printf("Enter the size for reallocation: ");
scanf("%d",&n1);
ptr1=(int *)realloc(ptr,(n+n1)*sizeof(int));
ptr=ptr1;
printf("Enter the elements for reallocation: ");
for(int i=n;i<n+n1;i++)
{
scanf("%d",&ptr1[i]);
}
// continue;
}
else if(choice==3) {
printf("The Elements are: ");
for(int i=0;i<n+n1;i++)
{
printf("%d",ptr[i]);
}
}
else if(choice==4) {
printf("Exit the program");
break;
}
else
{
printf("Invalid choice");
// continue;
}
}
}