The calloc function in C Programming

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int *ptr = calloc(1, sizeof(int));
    int *arrPtr = calloc(3, sizeof(int));
    if (ptr)
    {
        printf("The initial Value is : %d\n", *ptr);
        *ptr = 600;
        printf("New value is : %d\n", *ptr);
    }
    if (arrPtr)
    {
        printf("Intial Array Values:\n");
        for (int i = 0; i < 3; i++)
        {
            printf("%d ", arrPtr[i]);
        }
        printf("\n New Array Values:\n");
        for (int j = 0; j < 3; j++)
        {
            arrPtr[j] = (j + 2);
            printf("%d ", arrPtr[j]);
        }
    }
    free(ptr);
    free(arrPtr);

    return 0;
}

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *