C dynamic memory and ownership

malloc requests a block of uninitialized memory and returns NULL if it fails. Use sizeof *pointer to size elements consistently with the pointer type.

C
#include <stdio.h>
#include <stdlib.h>
int main(void) {
    int *values = malloc(3 * sizeof *values);
    if (values == NULL) return 1;
    for (int i = 0; i < 3; i++) values[i] = (i + 1) * 10;
    printf("%d\n", values[2]);
    free(values);
    values = NULL;
    return 0;
}
Read the full lesson