Home

A program to explain memory allocation

C Programming Language 112

Program

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

void main()
{
    int i, *p1, *p2, n;

    printf("Enter size: ");
    scanf("%d", &n);

    p1 = (int *)malloc(n * sizeof(int));
    p2 = (int *)calloc(n, sizeof(int));

    for (i = 0; i < n; i++)
    {
        scanf("%d %d", p1 + i, p2 + i);
    }

    for (i = 0; i < n; i++)
    {
        printf("%d\t", *(p1 + i));
    }
    printf("\n");

    for (i = 0; i < n; i++)
    {
        printf("%d\t", *(p2 + i));
    }

    printf("\n");
}

Result

Enter size: 3
1 2 3 4 5 6
1       3       5
2       4       6


Last Updated on

Next Post: Rules vs Guidelines →

Comments