Pointer is a variable that represents the
location of a data item, such as variable or an array element. Within the
computer’s memory, every stored data item occupies one or more contiguous memory
cells. The number of memory cells required to store a data item depends on the
type of the data item. For example, a single character will typically be stored
in one byte of memory; an integer usually requires two contiguous bytes, a
floating-point number usually requires four contiguous bytes, and a double
precision usually requires eight contiguous bytes.
Suppose v is a
variable that represents some particular data item. The compiler will
automatically assign memory cells to this data item. The data item can then be
accessed if we know the address of the first memory cell. The address of v’s
memory location can be determined by the expression &v, where & is the unary
operator, called the address operator, that evaluates the address of its
Now let us assign
the address of v to another variable pv. Thus pv = &v;
This new variable is called pointer to v. since it “points to” the location
where v is stored in address, not its value. Thus pv is referred to as a pointer
variable. The relationship between pv and v is illustrated in the following
Address of V-----------> Value of V
The data item
represented by v. (i.e. the data item stored in v’s memory cells) can be
accessed by the expression *pv where * is a unary operator called the indication
operator, that operates only on a pointer variable. Therefore, *pv and v both
represent the same data item.
The address operator
(&) and indirection operator(*) are unary operators and they are the members of
the same precedence group as the other unary operators. The address operator (&)
must act upon operands that associated with unique address, such as ordinary
variable or single array element. Thus the address operators cannot act upon
arithmetic expressions. The indirection operator can only act upon operands that
are pointers (e.g., pointer variables).
like all other variables, must be declared before, they may be used in C
program. When a pointer variable is declared, the variable name must be preceded
by an asterisk(*). This identifies the fact that the variable is a pointer. The
data type that appears in the declaration refers to the object of the pointer.
i.e. the data item that is stored in the address represented by the pointer,
rather than the pointer itself. Thus a pointer declaration may be written in
general terms as :
Where ptr is the name of the pointer variable, and data-type refers to the
data type of the pointer object.
For example, a C program contains the
The first line
declares i to be an integer type variable and ptri to be a pointer variable
whose object is an integer quantity. The second line declares f to be a
floating-point type variable and ptrf to be a pointer variable whose object is a
floating point quantity.
Within a variable
declaration, a pointer variable can be initialized by assigning in the address
of another variable, remember that the variable whose address is assigned to the
pointer variable must have been declared earlier in the program, for example,
The first line
declares i to be integer type variable and the second line declares ptri to be a
pointer variable whose object is an integer point quantity. In addition, the
address of i is initially assigned to ptri.
The following program illustrates the use of
Operation possible with pointers
C language allows
arithmetic operations performed on pointer variables. The arithmetic operations
available for use with pointer can be classified as
- Unary operator: ++ (increment) and --
- Binary operator; +(addition) and
The size of pointer
variable depends on the data type of the variable pointed to by the pointer.
If the pointer to an
integer is incremented using the ++ operator, then the address contained in the
pointer is incremented by two and not one, assuming that an integer occupies two
bytes in memory. Similarly if the pointer points to a floating point variable
the use of ++ operator increments the address by four. In general a pointer to
some data type d_type when incremented by an integer value i then the result
will have the following value.
The following program illustrates this.
I think you have already studied functions,
now let's see how it works with pointers.
If you have any doubt,Please ask an