Pointers

Variables that store memory addresses (useful for reading memory addresses).

Assuming that an int occupies 4 bytes:

int A;

The pointer must be defined with a type The address it contains points to the first byte of the group of N bytes in memory that defines the specified data type.

In C, a pointer is always associated with a specific data type, which determines how many bytes are used for the data it points to. For example, a pointer to an int points to the first byte of a memory block large enough to hold an int (typically 4 bytes).

Architecture
Address size
Number of possible addresses

64-bit

8 Bytes

( 2^{64} )

32-bit

4 Bytes

( 2^{32} )

Single Pointers

Inside the pointer variable, there is the memory address of a variable that can only contain variables of type type_name.

Possible declarations

The * in the DECLARATION:

  • does not modify the type

  • ties the concept of a pointer to the variable to its right

int* p; = int *p; = int * p;

Example:

  • p: pointer to integer value

  • i: integer variable

Pointer operators

Dereferencing

Unary operator to access the value stored at the memory address pointed to by a pointer

Returns the value of type type_name stored at the memory address pointed to by pointer_name

Memory address of a variable

Unary operator used to obtain the memory address of a variable

Example:

  • Initialization of a pointer (It cannot be done together with the declaration) In this case, the assignment becomes an initialization.

Example:

Segmentation Fault

  • If a pointer is not initialized, it contains whatever was previously stored in that memory cell

  • a pointer often contains memory addresses that the program cannot actually access (The program’s execution process is terminated)

Pointer arithmetic

Addition of an integer to a pointer

equals to:

where:

  • type_num_bytes: number of bytes for the type_name type pointed by the pointer variable

Example:

Subtraction of an integer from a pointer

equals to:

where:

  • type_num_bytes: number of bytes for the type_name type pointed by the pointer variable

Example:

Subtraction between pointers

The operation returns the number of variables (of the same type as the one pointed to by the pointer) that can fit in memory between the two variables pointed to by the pointers

This operations returns a number of type ptrdiff_t (that is a long long number) equal to:

Arrays & pointers

  1. Possible management of elements outside an array having an array of N elements, it is possible to access an element outside it (for example a[N]) with read privileges (no write privileges)

  2. Access to elements using indexing operator*(p+i) = p[i]

  3. Pointer to the array can not be modified

    a can not point to a[1] instead of a[0]

Double Pointers

It is a pointer variable that contains the memory address of another pointer to a variable of type type_name.

Example:

Function pointers

Memory address of the pointer as value of the Variable

Pointer declaration

  • First Method

  • Second Method

where:

  • returned_type: pointer type based on the data type returned by the function

  • parameter_types: parameters types, separated by commas

Function call with the pointer

  • First Method

  • Second Method

Retrieve the function pointer

It is enough to use the name of the function:

Last updated