< Previous | Contents | Next >

Initializing Pointers

As with other variables, you can initialize a pointer in the same statement you declare it. That’s what I do next with the following line, which assigns 0 to pScore.

int* pScore = 0; //declare and initialize a pointer‌

Assigning 0 to a pointer has special meaning. Loosely translated, it means, “Point to nothing.” Programmers call a pointer with the value of zero a null pointer. You should always initialize a pointer with some value when you declare it, even if that value is zero.


Hin t

image

Many programmers assign NULL to a pointer instead of 0 to make the pointer a null pointer. NULL is a constant defined in multiple library files, including iostream.

image