Wednesday, April 6, 2016

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. 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.
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.
Suppose 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, Thus:
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.

0 comments:

Post a Comment