Dynamic memory Allocation in c program
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *ptr;
int num;
printf("Enter the no of interger U want :\n");
scanf("%d", &num);
ptr = (int *)calloc(num, sizeof(int));
for (int i = 0; i < num; i++)
{
printf("Enter the value of element %d is :\n", i + 1);
scanf("%d", &ptr[i]);
}
for (int i = 0; i < num; i++)
{
printf("the value of element %d is: %d \n", i + 1, ptr[i]);
}
free(ptr);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int main()
{
float *ptr;
ptr = (float *)malloc(8 * sizeof(float));
ptr = realloc(ptr, 14 * sizeof(float));
free(ptr);
for (int i = 0; i < 8; i++)
{
printf("Enter the value of element %d is :\n", i + 1);
scanf("%f", &ptr[i]);
}
for (int i = 0; i < 5; i++)
{
printf("the value of element %d is %f \n", i + 1, ptr[i]);
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int main()
{
float *ptr;
ptr = (float *)malloc(8 * sizeof(float));
for (int i = 0; i < 8; i++)
{
printf("Enter the value of element %d is :\n", i + 1);
scanf("%f", &ptr[i]);
}
for (int i = 0; i < 5; i++)
{
printf("the value of element %d is %f \n", i + 1, ptr[i]);
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *ptr;
ptr = (int *)malloc(10 * sizeof(int));
printf("Multiplication Table of Seven is :\n");
for (int i = 0; i < 10; i++)
{
printf(" %d*%d=%d\n", 7, i + 1, 7 * (i + 1));
}
printf("printing table using realloc memory\n");
ptr = realloc(ptr, 15 * sizeof(int));
for (int i = 0; i < 15; i++)
{
printf(" %d*%d=%d\n", 7, i + 1, 7 * (i + 1));
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *ptr;
ptr = (int *)malloc(5 * sizeof(int));
for (int i = 0; i < 5; i++)
{
printf("Enter the value of element %d is :\n", i + 1);
scanf("%d", &ptr[i]);
}
for (int i = 0; i < 5; i++)
{
printf(" the value of element %d is :%d\n", i + 1, ptr[i]);
}
ptr = realloc(ptr, 10 * sizeof(int));
for (int i = 0; i < 10; i++)
{
printf("Enter the value of element %d is :\n", i + 1);
scanf("%d", &ptr[i]);
}
for (int i = 0; i < 10; i++)
{
printf(" the value of element %d is :%d\n", i + 1, ptr[i]);
}
return 0;
}
No comments:
Post a Comment