realloc() Function in C
Get Certified in C Programming and Take Your Skills to the Next Level
Program 1
//Program for realloc function
#include<stdio.h>
#include<conio.h>
#include <stdlib.h>
#define clrscr() system("cls")
int main()
{
int *ptr=NULL;
ptr=(int *)malloc(2*sizeof(int));
// clrscr();
if(ptr==NULL)
{
printf("Memory not allocated");
//exit(0);
}
else
{
printf("Enter two Number");
for(int i=0;i<2;i++)
{
scanf("%d",ptr+i);
}
ptr=(int*)realloc(ptr,4*sizeof(int));
if(ptr==NULL)
{
printf("Memory not allocated");
//exit(0);
}
else
{
printf("Enter next two Number");
for(int i=2;i<4;i++)
{
scanf("%d",ptr+i);
}
int sum=0;
printf("Elements are: ");
for(int i=0;i<4;i++)
{
printf("%d\n",*(ptr+i));
sum=sum+*(ptr+i);
}
printf("Sum is %d",sum);
free(ptr);
}
}
}
Did you like our efforts? If Yes, please give DataFlair 5 Stars on Google

