githubEdit

C

Introduction

Types

Conditional

Iteration

Memory management

Pointers

A pointer is a variable that stores a memory location (or an address of a variable).

datatype *ptr_name;

Example:

int *ptr;

Operators

* (asterisk)

The unary operator * (asterisk) is used for two things:

  • To declare a pointer variable (see example above);

  • To access the value stored in a pointer. This is called "deferencing".

& (ampersand)

To get the address of a variable, we use the unary operator & (ampersand). For example, &x gives us the address of variable x.

Example:

Dynamic memory allocation

malloc()

Allocate memory in the heap.

Example:

calloc()

Allocate memory in the heap and initialize it to zero.

Example:

realloc()

Reallocate memory in the heap.

Example:

free()

Free memory in the heap.

Tips

  • Always check if allocation was successful:

  • It must be as mallocs or callocs as frees;

  • Don't forget to count the null terminator when allocating memory for strings.

Functions

Structures

Multi-file

Last updated