← The C Language Lesson 48 of 77 · 5 XP

Warm-up: a number on the heap warm-up

Theory and problem statement

Machine translation, not yet reviewed by a native speaker.

malloc(size) allocates a piece of memory and returns a pointer to it. When the memory is no longer needed, you give it back with free:

int *p = malloc(sizeof(int));   // room for one int
*p = 42;                        // * means "what p points to"
printf("%d\n", *p);
free(p);

malloc is declared in <stdlib.h>.

Task. Allocate memory for one int, read a number into it, print it multiplied by 3, and free the memory.

Sign up for free to solve this task right in your browser: code editor, running, test checks and hints.