Syntax

C Allocate Memory Syntax

In C, allocate memory: allocates memory on the heap — int *a = malloc(n * sizeof *a);

Quick Answer

Syntax
int *a = malloc(n * sizeof *a);
CSyntax

Allocate Memory

Code example
int *a = malloc(n * sizeof *a);

Allocates memory on the heap.

Learn More

How to allocate memory in C

  1. Open your C file or editor.
  2. Copy the example, then replace its sample names and values for your code.

C Allocate Memory Syntax

Use int *a = malloc(n * sizeof *a);.

Related Syntax References

Frequently asked questions

What is the C syntax for allocate memory?

Use this pattern: int *a = malloc(n * sizeof *a);

Can I copy this C code example?

Yes. Use Copy code and adapt the example to your program.

Is this C syntax different on Mac or Windows?

No. The language syntax is the same across operating systems.

Official source · How we verify