Showing posts with label calloc. Show all posts
Showing posts with label calloc. Show all posts

Monday 1 August 2016

Dynamic Memory Allocation

Module 1: Dynamic Memory Allocation


  • free()

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);
}