C Language - Malloc, Calloc, Realloc - use
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
//int i;
//int j;
//int *ptr;
//use of malloc
//printf ("Enter the size of the array \n");
//scanf ("%d", &j);
//ptr = (int *) malloc (j* sizeof(int));
//for (i=0; i<j; i++)
//{
// printf ("Enter a value of array %d\n", i);
// scanf ("%d", &ptr[i]);
//} for (i=0; i<j; i++)
//{
// printf ("The value at %d is %d\n", i, ptr[i]);
//}
//use of calloc....
int i;
int j;
int *ptr;
printf ("Enter the size of the array \n");
scanf ("%d", &j);
ptr = (int *) calloc (5, sizeof(int));
for (i=0; i<5; i++)
{
printf ("Enter a value of array %d\n", i);
scanf ("%d", &ptr[i]);
}
for (i=0; i<5; i++)
{
printf ("The value at %d is %d\n", i, ptr[i]);
}
//use of realloc...
printf ("Enter new size of the array you want \n");
scanf ("%d", &j);
ptr = (int *) realloc (ptr, 5* sizeof(int));
for (i=0; i<5; i++)
{
printf ("Enter a value of array %d\n", i);
scanf ("%d", &ptr[i]);
}
for (i=0; i<5; i++)
{
printf ("The value at %d is %d\n", i, ptr[i]);
}
//use of free...
return 0;
}
// ScreenShots:
Comments
Post a Comment