Code Sharing: Simplest Dynamic Memory Allocation with malloc in C

// Simplest dynamic memory allocation
#include <stdio.h>
#include <stdlib.h> // stdlib.h must be included for malloc
int main(void) {
    int *n;
    n = (int*)malloc(sizeof(int));  // Allocate one byte of memory for pointer n
    *n = 100;  // Assign value to *n
    // scanf("%d", n);
    printf("%d", *n);
    free(n);  // Free memory
    return 0;
}

Leave a Comment