Dynamic Memory Allocation in C program Example

How to write a C program Dynamic allocation program in C programming (Example) ?

dynamic memory allocation refers to performing manual memorymanagement for dynamic memory allocation in the C programming language via a group of functions in the C standard library, namely malloc , realloc , calloc and free .

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

void swap(int *a, int *b);

int main()
{
    int *x, n, i;
 
    printf("How many numbers?\n");
    scanf("%d", &n);
    x = (int *)malloc(n*sizeof(int));
 
    for(i = 0; i < n; i++)
    scanf("%d", &x[i]);
   
    free(x);
 
    return 0;
}

void swap(int *a, int *b)
{
int temp = *a;
*a = *b;
*b = temp;
}


Learn More :