Check out example codes for "malloc in c". It will help you in understanding the concepts better.

Code Example 1

//malloc or "memory allocation" reserves a part of the memory

pointer = (cast-type*) malloc(byte-size)
ptr = (int*) malloc(100 * sizeof(int));

Since the size of int is 4 bytes, this statement will allocate 400 bytes
of memory. And, the pointer ptr holds the address of the first byte in
the allocated memory.

Code Example 2

#include <stdlib.h>

void *malloc(size_t size);

void exemple(void)
  char *string;
  string = malloc(sizeof(char) * 5);
  string[0] = 'H';
  string[1] = 'e';
  string[2] = 'y';
  string[3] = '!';
  string[4] = '\0';
  printf("%s\n", string);

/// output : "Hey!"

Code Example 3

ptr = (cast-type*) malloc(byte-size)

Code Example 4

ptr = (cast-type*)calloc(n, element-size);

Code Example 5


Learn ReactJs, React Native from