Dynamic Memory Allocation: malloc()
Download the ProgramSyntax:
ptr = (cast_type *) malloc(size_in_bytes);
#include<stdio.h>
void main()
{
int i, n;
int *arr;
printf("\nEnter the number of elements: ");
scanf("%d", &n);
arr = (int *)malloc(n*sizeof(int));
if(arr == NULL)
{
printf("\nInsufficient memory allocation");
exit(0);
}
printf("\nEnter the array elements: ");
for(i=0; i<n; i++)
scanf("%d", (arr+i));
printf("\nThe array elements are: ");
for(i=0; i<n; i++)
printf("%d ", *(arr+i));
free(arr);
}
Output:
Enter the number of elements: 5
Enter the array elements: 11 22 33 44 55
The array elements are: 11 22 33 44 55
Also Check,
https://tejaswinihbhat.blogspot.in/2016/08/dynamic-memory-allocation.html
https://tejaswinihbhat.blogspot.in/2016/08/malloc-dynamic-memory-allocation.html
https://tejaswinihbhat.blogspot.in/2016/08/calloc-dynamic-memory-allocation.html
https://tejaswinihbhat.blogspot.in/2016/08/realloc-dynamic-memory-allocation.html
No comments:
Post a Comment