C Program Of Realloc(C library function)

C Program Of Realloc(C library function)

#include<stdio.h>
#include<stdlib.h>

main()
{

int i,n,*ptr,*p,sum=0,*q;

printf("Enter array size:\n");
scanf("%d",&n);

ptr=(int*)malloc(n*sizeof(int));
p=ptr;

printf("Allocated Memory %p\n",ptr);


if(ptr==NULL)
{

printf("Error in creating Memory\n");
exit(0);

}

printf("Enter array elements:\n");

for(i=0;i<n;i++)
{

scanf("%d",ptr);

sum=sum+*ptr;
ptr++;

}


for(i=0;i<n;i++)

{

printf("%d\n",*p);
p++;

}

printf("Sum is :%d\n",sum);


printf("Enter new array size:\n");
scanf("%d",&n);


ptr=(int*)realloc(ptr,n*sizeof(int));


q=ptr;


printf("Enter array elements:\n");

for(i=0;i<n;i++)
{

scanf("%d",ptr);

sum=sum+*ptr;
ptr++;

}

for(i=0;i<n;i++)

{

printf("%d\n",*q);
q++;

}

printf("Sum is :%d\n",sum);

}


Related Posts