-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dynamic_Memory_realloc.c
42 lines (37 loc) · 1007 Bytes
/
Dynamic_Memory_realloc.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
#include <stdio.h>
#include <conio.h>
#include <stdlib.h> // malloc,calloc,realloc,free
int main()
{
int *ptr;
int n;
printf("Enter the size of array you want to create: ");
scanf("%d", &n);
ptr = (int *)calloc(n, sizeof(int));
for (int i = 0; i < n; i++)
{
printf("Enter the value no: %d of this array\n", i);
scanf("%d", &ptr[i]);
}
for (int i = 0; i < n; i++)
{
printf("The value at %d of this array is %d \n", i, ptr[i]);
// scanf("%d",&ptr[i]);
}
// Realloc
printf("Enter the size of new array you want to create: ");
scanf("%d", &n);
ptr = (int *)realloc(ptr, n * sizeof(int));
for (int i = 0; i < n; i++)
{
printf("Enter the value no: %d of this array\n", i);
scanf("%d", &ptr[i]);
}
for (int i = 0; i < n; i++)
{
printf("The value at %d of this array is %d \n", i, ptr[i]);
// scanf("%d",&ptr[i]);
}
free(ptr);
return 0;
}