write a program to reallocate the memory dynamically using re-alloc method .
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int main()
{
int n,i;
int *ptr;
printf("enter the no. of elements:");
scanf("%d",&n);
ptr=(int*)calloc(n,sizeof(int));
printf("memory allocated using calloc\n");
for(i=0;i<n;i++)
{
ptr[i]=i+1;
}
printf("elements in the array are\n");
for(i=0;i<n;i++)
{
printf("%d\n",ptr[i]);
}
printf("enter the new size:");
scanf("%d",&n);
ptr=realloc(ptr,n*sizeof(int));
printf("memory reallocated using realloc\n");
for(i=0;i<n;i++)
{
ptr[i]=i+1;
}
printf("elements of array are\n");
for(i=0;i<n;i++)
{
printf("%d\n",ptr[i]);
}
return 0;
}
Comments
Post a Comment