Sei sulla pagina 1di 5

Pointers

8.1. INTRODUCTION
Pointer is a variable that holds a memory address, usually the location of
another variable in memory. The pointers are one of Cs most useful and strongest
features. Let i be an ordinary variable that stores the value of an integer. If p is
another variable used to store the address of variable i, then p is called a pointer
variable, pointing to i. A pointer must be preceded by an asterisk !", #hile
defining it, so that it is distinct from other variables. The definition of a pointer
variable must also specify the type of data stored in the target variable pointed to
it. In our e$ample, the target variable is i, is holding integer type data, and hence,
the pointer variable p must be defined as of the type int.
%ere, i is the name of a variable that stores the value &' and is stored in
memory in the address, say &()*. This could be represented as follo#s +
&()* Address of the variable i
&' ,alue of the variable i
-o# consider another variable, p, #hich holds the address of the variable i.
Then p is said to be a pointer pointing to i. This again, could be graphically
represented as +
&()* ........... &'
p i
8.2. ADVANTAGES OF USING POINTERS
There are number of advantages of using pointers. They are +
&. Pointers increase the speed of e$ecution of a program.
(. Pointers reduce the length and comple$ity of a program.
). Pointers are more efficient in handling the data tables.
*. A pointer enables us to access a variable that is defined outside the
function.
/. The use of a pointer array to character strings results in saving of data
storage space in memory.
8.2 Pointers
8.3. DECLARING POINTERS
Like all variables, a pointer variable should also be declared. The synta$ is +
data_type *ptr_variable ;
0here data1type is the type of the pointer variable ptr1variable" #hich may
of any valid type. ! is called as the indirection operator also referred to as the
de.referencing operator, #hich states that the variable is not an ordinary variable
but a pointer variable. The ptr1variable is the name of the pointer variable. The
rules for naming pointer variables are same as ordinary variables.
2or e$ample +
int !ptr 3
0ill declare the variable ptr as a pointer variable that points to an integer
data type.
8.4. INITIALIZING POINTERS
It is al#ays good practice to initiali4e pointers as soon as it is declared. 5ince
a pointer is 6ust an address, if it is not initiali4ed, it may randomly point to some
location in memory. The ampersand 7" symbol, also called address operator, is
applied to a variable to refer the address of that variable. Initiali4ing pointers can
be made to point to a variable using an assignment statement. The synta$ is +
ptr_variable = &variable ;
%ere, the address of the variable is assigned to ptr1variable as its value. 2or
e$ample +
ptr 8 7price 3
0ill cause ptr to point to price i.e., ptr no# contain the address of price. A
pointer variable can be initiali4ed in its declaration itself. 2or e$ample +
int price, !ptr 8 7price 3
Is also valid.
Pointers 8.3
8.. ACCESSING A VARIA!LE T"ROUG" ITS POINTER
Accessing the value of the variable using pointer is done by using unary
operator ! asterisk", usually kno#n as indirection operator. Consider the
follo#ing statements +
int price, !ptr, n 3
price 8 &'' 3
ptr 8 7price 3
n 8 !ptr 3
The first line declares the price and n as integer variables and ptr as a pointer
variable pointing to an integer. The second line assigns the value &'' to price and
third line assigns the address of price to the pointer variable ptr. The fourth line
contains the indirection operator !. 0hen the operator ! is placed before a pointer
variable in an e$pression on the right hand side of the e9ual sign", the pointer
returns the value of the variable of #hich the pointer value is the address.
In this case, !ptr returns the value of the variable price, because ptr is the
address of price. The ! can be remembered as value at address. Thus the value of
n #ould be &''.
Consider the follo#ing program for accessing a variable through a pointer +
#$ De%onstr&tion o' &((essin) & *&ri&+,e t-ro.)- /ointer $#
0 in(,.1e 2st1io.-3
0 in(,.1e 2(onio.-3
*oi1 %&in45
6
int & 7 189 + 7 289 $/tr :
(,rs(r45 :
/tr 7 ;& :
/rint'4<Initi&, *&,.e o' & is = >1 &n1 + is = >1?n?n<9 &9 +5 :
+ 7 $/tr :
/rint'4<C-&n)e1 *&,.e o' & is = >1 &n1 + is = >1?n?n<9 &9 +5 :
/rint'4<V&,.e o' /tr is = >.?n?n<9 /tr5 :
/rint'4<V&,.e o' t-e &11ress o' & is = >.?n?n<9 ;&5 :
/rint'4<V&,.e o' $/tr is = >1<9 $/tr5 :
)et(-45 :
@
8.4 Pointers
RUN 1 =
Initi&, *&,.e o' & is = 18 &n1 + is = 28
C-&n)e1 *&,.e o' & is = 18 &n1 + is = 18
V&,.e o' /tr is = A24
V&,.e o' t-e &11ress o' & is = A24
V&,.e o' $/tr is = 18
8.A. POINTER ARIT"BETIC
An interesting use of pointers is pointer arithmetic. The arithmetic operations
that are performed on pointers are addition and subtraction. :ou can use the
increment ;;" and decrement .." operators on a pointer to move the pointer in
memory. The number of bytes the pointer moves be depends on the si4e of the
variable pointed by the pointer. The length of various data types are as sho#n
belo# +
Data Type Length
char & byte
int ( byte
float * byte
long * byte
double < byte
This length is called the scale factor. %o#ever an e$pression like +
ptr;; 3
0ill cause the pointer ptr to point to the ne$t value of its type. 2or e$ample, if
ptr is an integer pointer #ith an initial value, say &/'', then after the operation,
the value of ptr #ill be &/'(, and not &/'&, because the length of integer data
type is (. 5imilarly, if ptr is a character pointer, the increment operation moves
the pointer by one byte from #here it is previously pointed, because the length of
character data type is &.
If a decrement operation is carried out on a pointer, the pointer moves
back#ards, depending upon the si4e of the variable pointed by the pointer.
%o#ever an e$pression like +
ptr.. 3
Pointers 8.5
0ill cause the pointer ptr to point to the previous value of its type. 2or
e$ample, if ptr is an integer pointer #ith an initial value, say &/'', then after the
operation, the value of ptr #ill be &*=<, and not &*==, because the length of
integer data type is (. 5imilarly, if ptr is a character pointer, the decrement
operation moves back the pointer by one byte from #here it is previously pointed,
because the length of the character data type is &.
Consider the follo#ing program for finding the length of a string through a
pointer +
#$ De%onstr&tion o' /ointer &rit-%eti( $#
0 in(,.1e 2st1io.-3
0 in(,.1e 2(onio.-3
*oi1 %&in45
6
(-&r $n&%e9 $(o.nt :
int $/tr9 ,en :
(,rs(r45 :
/rint'4<Enter & strin) = <5 :
s(&n'4<>s<9 n&%e5 :
(o.nt 7 n&%e :
C-i,e4$(o.nt D7 E?8E5
(o.ntFF :
,en 7 (o.nt G n&%e :
/rint'4<?nT-e ,en)t- o' t-e strin) is = >1<9 ,en5 :
)et(-45 :
@
RUN 1 =
Enter & strin) = Uni*ersitH
T-e ,en)t- o' t-e strin) is = 18