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).
64-bit
8 Bytes
( 2^{64} )
32-bit
4 Bytes
( 2^{32} )
Single Pointers
type_name *pointer_name;
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:
int* p, i;
p
: pointer to integer valuei
: integer variable
Pointer operators
Dereferencing
Unary operator to access the value stored at the memory address pointed to by a pointer
*pointer_name
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
& variable_name
Example:
int *p,a;
p=&a;
Initialization of a pointer (It cannot be done together with the declaration) In this case, the assignment becomes an initialization.
Example:
int A;
A=25;
int* p;
p=&A;

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
pointer_name + integer_number
equals to:
pointer_name + (integer_number * type_num_bytes)
where:
type_num_bytes
: number of bytes for thetype_name
type pointed by the pointer variable
Example:
int *p, *q, a;
p=&a;
q=p+4;

Subtraction of an integer from a pointer
pointer_name - integer_number
equals to:
pointer_name + (integer_number * type_num_bytes)
where:
type_num_bytes
: number of bytes for thetype_name
type pointed by the pointer variable
Example:
int *p, *q, a;
p=&a;
q=p-4;

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
pointer_name1 - pointer_name2
This operations returns a number of type ptrdiff_t
(that is a long long
number) equal to:

Arrays & pointers
int a[9], *p, *q, *r;
r=&a[5];
q=&a[0];
p=a;
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)Access to elements using indexing operator
*(p+i)
=p[i]
Pointer to the array can not be modified
int a[10]; a++;
a
can not point toa[1]
instead ofa[0]

Double Pointers
It is a pointer variable that contains the memory address of another pointer to a variable of type type_name
.
type_name **pointer_name;
Example:
int A;
A=25;
int* p1;
int** p2;
p1=&A;
p1=&p2;
Function pointers
Memory address of the pointer as value of the Variable
Pointer declaration
First Method
returned_type (*pointer_name)(parameter_types);
Second Method
returned_type pointer_name(parameter_types);
where:
returned_type
: pointer type based on the data type returned by the functionparameter_types
: parameters types, separated by commas
Function call with the pointer
First Method
pointer_name(arguments);
Second Method
(*pointer_name)(arguments);
Retrieve the function pointer
It is enough to use the name of the function:
void f(int a)
{
…
}
void (*pf)(int);
pf=f;
Last updated