USE OF MALLOC() IN C
#include <stdio.h>
#include <stdlib.h>
int main()
{
// // use of malloc
int *ptr, n;
printf("Enetr the sie of the array you want to create\n");
scanf("%d", &n);
ptr = (int *)malloc(n * sizeof(int));
for (int i = 0; i < n; i++)
{
printf("Enter the value of no %d of this array\n",i);
scanf("%d", &ptr[i]);
}
for (int i = 0; i < n; i++)
{
printf("The value at %d of this array is %d\n",i,ptr[i]);
}
free(ptr);
return 0;
}
Enetr the sie of the array you want to create
5
Enter the value of no 0 of this array
78
Enter the value of no 1 of this array
56
Enter the value of no 2 of this array
45
Enter the value of no 3 of this array
45
Enter the value of no 4 of this array
34
The value at 0 of this array is 78
The value at 1 of this array is 56
The value at 2 of this array is 45
The value at 3 of this array is 45
The value at 4 of this array is 34
Comments
Post a Comment