Friday, 9 November 2012


//C code for Example of Dynamic memory reallocation
#include<stdio.h>
#include<alloc.h>
main()
{
int *mark;
int n=0,i,mr,size=3;
clrscr();
mark=(int *)malloc(size*sizeof(int));
printf("Enter the marks:(Enter -1 to stop)\n");
scanf("%d",&mr);
while(mr!=-1)
{
if(n>=size)
{
size=size+3;
printf("\nEnter more than 3 integer\n");
mark=realloc(mark,size*sizeof(int));
if(!mark)
{
printf("\nNot enough memory\n");
}
else
{
printf("\nSuccess\n");
}
}
*(mark+n)=mr;
scanf("%d",&mr);
n++;
}
printf("\nThe marks entered are:\n");
for(i=0;i<n;i++)
printf("\n%d",*(mark+i));
getch();
}

No comments:

Post a Comment