[Solved] Why should I use pointers? C++


A pointer is an address of a variable.

Imagine ram as consecutive boxes. Each box has an address.

Now in your example.

int a = 8; //declares a variable of type int and initializes it with value 8

int *p1; //p1 is a pointer meaning that the variable p1 holds an address instead of a value.

p1 = &a; //The operator &is called address of. This statement makes p1 point to the address of a. That is p1 holds the address of a.

In order to access the value of a instead of the address you need to deference it.

*p1 = 9; //now a = 9

2

solved Why should I use pointers? C++