Monday, 1 August 2016

calloc() Dynamic Memory Allocation

Dynamic Memory Allocation: calloc()

ptr = (cast_type  *) calloc (number_of_blocks, size_in_bytes);


#include<stdio.h>
void main()
{
    int i,n;
    int *arr;

    printf("\nEnter the number of elements: ");
    scanf("%d", &n);

    arr = (int *)calloc(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);
}

No comments:

Post a Comment