#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
void mallocExample();
void callocExample();
void reallocExample();
void main()
{
mallocExample();
callocExample();
reallocExample();
getch();
}
void mallocExample()
{
int n,*ptr,i;
printf("MALLOC\nEnter how many space wanna reserve for int:: ");
scanf("%d",&n);
ptr=(int*)malloc(n*sizeof(int));
printf("Reserved memories and default values are:\n");
for(i=0;i<n;i++)
printf("%u\t%d\n",ptr+i,*(ptr+i));
free(ptr);
}
void callocExample()
{
int n,*ptr,i;
printf("\n\nCALLOC\nEnter how many space wanna reserve for int:: ");
scanf("%d",&n);
ptr=(int*)calloc(n,sizeof(int));
printf("Reserved memories and default values are:\n");
for(i=0;i<n;i++)
printf("%u\t%d\n",ptr+i,*(ptr+i));
free(ptr);
}
void reallocExample()
{
int n1,n2,*ptr,i;
printf("\n\nREALLOC\nEnter how many space wanna reserve for int:: ");
scanf("%d",&n1);
ptr=(int*)calloc(n1,sizeof(int));
printf("Reserved memories and default values are:\n");
for(i=0;i<n1;i++)
printf("%u\t%d\n",ptr+i,*(ptr+i));
free(ptr);
printf("Enter new size:: ");
scanf("%d",&n2);
realloc(ptr,n2);
printf("Again, Reserved memories and default values are:\n");
for(i=0;i<n2;i++)
printf("%u\t%d\n",ptr+i,*(ptr+i));
free(ptr);
}
OUTPUT:
No comments:
Post a Comment