Basics of Pointers in C++
Before we start with basics of pointers, it is strictly recommended to go through the concepts of pointers from the Learn C Online (Click here). We have attempted to cover pointers in the most simplest manner possible. But, it will be impossible for you to grasp the knowledge of the pointers unless and until you have the concepts of pointers clear in your mind by reading – Learn C Online (Click here).
A pointer is a variable that represents the location (rather than the value) of a data item, such as a variable or an array element.
v is a variable that represents some particular data item. The compiler will automatically assign memory cells for this data item. The data item can then be accessed if we know the location (i.e. the address) of the first memory cell allocated to variable
v by the compiler. The address of
v‘s memory location can be determined by the expression:
where & is a unary operator, called the address operator, that evaluates the address of the operand.
Now, let us assign the address of
v to another variable,
pv = &v
The new variable (pv) is called a pointer to v, since it “points” to the location where
v is stored in memory. Remember, however, that
pv represents v’s address, not its value. Thus,
pv is referred to as pointer variable. The data item represented by
v (i.e., the data item stored in the v’s memory cells) can be accessed by the expression:
where * is a unary operator, called the indirection operator.