Select Page

What are pointers?

by 02.03.2023C pointers

Pointer is a variable which stores the address of a memory location. The pointer can represent the address and the type of a variable or of a function.

The key to understand the pointers is to learn how memory is managed in a C program.

Types of memory

We work with three types of memory when a C program is compiled:

  1. Static / Global memory,
  2. Automatic memory,
  3. Dynamic memory.

Static / Global memory

When we work with statically declared variables, it means that the variables are allocated when the program starts and they remain in the memory till the program is terminated.

Global variables can be accessed by all the functions. Static variables can be accessed only by their defining functions.

The lifetime of static and global variables is limited to the lifetime of the application.

Automatic memory

Automatic variables are declared within a function and are initiated when a function is called. The automatic variables can be accessed by their defining functions and their lifetime is limited while the function is executing.

Dynamic memory

The dynamic memory is allocated from the heap. The heap manages dynamic memory. The lifetime of the dynamic variables ends when the memory is freed. The scope of dynamic variables is determined by the pointers.

Very short summary about pointers

Pointers are mostly used to manipulate data in memory. The pointer only contains the address in memory of something. This something can be another variable, object or function.

The object can be any C data type, such as integer, character, string or structure.

Sources:
[1] Understanding and Using C Pointers by Richard Reese (O’Reilly). Copyright 2013 Richard Reese, Ph.D. 978-1-449-34418-4
[2] PRINZ, Peter a Ulla KIRCH-PRINZ. C Pocket Reference. O’REILLY, 2003. ISBN 978-0-596-00436-1.