PointersPointers

A variable of a pointer type is declared like a regular variable, but the data type will be with the * symbol, for example:

Note

Names of pointers are usually called with the letter "p" (pointer).

Modifying the example from the previous section:

cpp

main.cpp

The type of the pointer and the type of the value it points to must be the same.

The pointer variable contains only the address! Therefore, if we try to output a pointer to the console, the program will display the address.

If the pointer is an address (&variable), then to "pass to the address", you need to use the "*" operator.

cpp

main.cpp

If you don't initialize a pointer, it will contain a random value (in our case - random memory area), so pointers are grounded before use with a nullptr (NULL pointer) value.

Note

In the first example, we didn't use nullptr because we gave the pointer an address on the next line.

Everything was clear?

Section 6. Chapter 3