-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemoryallocusingrealloc.c
More file actions
37 lines (35 loc) · 897 Bytes
/
memoryallocusingrealloc.c
File metadata and controls
37 lines (35 loc) · 897 Bytes
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
#include<stdio.h>
#include<stdlib.h> //required for using realloc() in C
int main()
{
int *ptr;
int i;
// typecasting pointer to integer
ptr= (int *)calloc(4,sizeof(int));
if(ptr!=NULL)
{
for(i=0;i<4;i++)
{
printf("Enter number number %d: ", i+1);
scanf("%d",(ptr+i));
}
}
//reallocation of 6 elements
ptr= (int *)realloc(ptr,6*sizeof(int));
if(ptr!=NULL)
{
printf("\nNew memory allocated!\n");
for(;i<6;i++)
{
printf("Enter new number %d: ",i);
scanf("%d",(ptr+i));
}
}
printf("\n\nThe numbers are:\n");
for(i=0;i<6;i++)
{
printf("%d \n",ptr[i]);
}
free(ptr);
return 0;
}