malloc array c code example

Example 1: malloc int array c

int array_length = 100;
int *array = (int*) malloc(array_length * sizeof(int));

Example 2: c allocate array

int *array = malloc(10 * sizeof(int));

Example 3: allocate memory c

// Use malloc to allocate memory
ptr = (castType*) malloc(size);
int *exampl = (int*) malloc(sizeof(int));
// Use calloc to allocate and inizialize n contiguous blocks of memory
ptr = (castType*) calloc(n, size);
char *exampl = (char*) calloc(20, sizeof(char));

Example 4: c malloc array

#define ARR_LENGTH 2097152
int *arr = malloc (ARR_LENGTH * sizeof *arr);

Tags:

C Example