This is important topic
#include <stdio.h>
#include <stdlib.h> //calloc malloc realloc and free are defined inside this header file
int main()
{
// use of malloc
int *ptr;
int n;
printf("Enter the size of teh array you want to create :");
scanf("%d",&n);
ptr = (int *)malloc(n * sizeof(int)); // create dynamic array of 10 size
for (int i = 0; i < n; i++)
{
printf("Enter the value no %d of this :", i);
scanf("%d", &ptr[i]);
}
for (int i = 0; i < n; i++)
{
printf("The value %d of this array is %d\n", i, ptr[i]);
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *ptr;
int n;
ptr = (int *)calloc(n, sizeof(int));
printf("Enter the size of teh array you want to create :");
scanf("%d", &n);
for (int i = 0; i < n; i++)
{
printf("Enter the value no %d of this :", i);
scanf("%d", &ptr[i]);
//in calloc if we don't initailize it with any value it assigns 0
//so initializing is a extra task here that's why malloc is prefered
}
for (int i = 0; i < n; i++)
{
printf("The value %d of this array is %d\n", i, ptr[i]);
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int main()
{
// this all was allocated using calloc
int *ptr;
int n;
ptr = (int *)calloc(n, sizeof(int));
printf("Enter the size of teh array you want to create :");
scanf("%d", &n);
for (int i = 0; i < n; i++)
{
printf("Enter the value no %d of this :", i);
scanf("%d", &ptr[i]);
// in calloc if we don't initailize it with any value it assigns 0
// so initializing is a extra task here that's why malloc is
prefered
}
for (int i = 0; i < n; i++)
{
printf("The value %d of this array is %d\n", i, ptr[i]);
}
// from here we will allocate memory using realloc
ptr = (int *)realloc(ptr,n*sizeof(int));
printf("Enter the size of the new array you want to create :");
scanf("%d", &n);
for (int i = 0; i < n; i++)
{
printf("Enter the new value no %d of this :", i);
scanf("%d", &ptr[i]);
}
for (int i = 0; i < n; i++)
{
printf("The new value %d of this array is %d\n", i, ptr[i]);
}
return 0;
}
#include <stdio.h>
#include <stdlib.h> //calloc malloc realloc and free are defined inside this header file
int main()
{
// use of malloc
int *ptr;
int n;
ptr = (int *)malloc(n * sizeof(int)); // create dynamic array of 10 size
printf("Enter the size of teh array you want to create :");
scanf("%d",&n);
for (int i = 0; i < n; i++)
{
printf("Enter the value no %d of this :", i);
scanf("%d", &ptr[i]);
}
for (int i = 0; i < n; i++)
{
printf("The value %d of this array is %d\n", i, ptr[i]);
}
free(ptr);
return 0;
}