You are on page 1of 3

Training on C Programming Date : 20

th
-24
th
July
98
Topic : Advance-Pointer Page !o : " o# $
Advanced Pointers
Pointers to pointers
int %&'4( can alo )e repreented a
int %%&*
+n thi cae & repreent the array o# integer pointer ,
The i-e o# the array i allocated )y #unction malloc./ *
& 0 int %% malloc.4/* 1% & i capa)le o# holding 4 integer pointer %1
%. &2"/ 0 .int %)malloc(sizeof (int));
/ % memory i allocated #or #irt integer pointer %1
%. &22/ 0 .int %)malloc(sizeof (int));
/ % memory i allocated #or econd integer pointer %1

Two dimensional array
+nitiali-ation o# t3o dimenional array
int tud'4('2( 0 4
4 "2""5 $4 6
4 "$475 78 6
4 $4785 89 6
4 $2475 87 6
6*
or tud'4('2( 0 4 "2""5 $45 "$475 785 $4785 895 $2475 87 6*
Important :
+t i important to remem)er that 3hile initiali-ing an array it i neceary to mention the econd
.column/ dimenion5 3herea the #irt dimenion .ro3/ i optional ,Thu the declaration 5
int arr'2('$( 0 4"25$45225$45$$5896*
int arr' ('$( 0 4"25$45225$45$$5896*
are per#ectly accepta)le ,
3herea are unaccepta)le,
int arr'2(' ( 0 4"25$45225$45$$5896*
int arr' (' ( 0 4"25$45225$45$$5896*
What is N!! pointer "
The language de#inition tate that #or each pointer type5 there i a pecial value -- the :null
pointer: -- 3hich i ditinguiha)le #rom all other pointer value and 3hich i :guaranteed to
compare une;ual to a pointer to any o)&ect or #unction,: That i5 the addre-o# operator < 3ill
never yield a null pointer5 nor 3ill a ucce#ul call to malloc./, malloc./ doe return a null
pointer 3hen it #ail5 and thi i a typical ue o# null pointer: a a :pecial: pointer value 3ith
ome other meaning5 uually :not allocated: or :not pointing any3here yet,:/
A null pointer i conceptually di##erent #rom an un-initiali-ed pointer, A null pointer i =no3n
not to point to any o)&ect or #unction* an un-initiali-ed pointer might point any3here, A
mentioned a)ove5 there i a null pointer #or each pointer type5 and the internal val#es of n#ll
pointers for different types may $e different% Although programmer need not =no3 the
internal value5 the compiler mut al3ay )e in#ormed 3hich type o# null pointer i re;uired5 o
that it can ma=e the ditinction i# neceary
Pointer to f#nction
+t i not #re;uently ued,
1var13331app1converion1tmp1cratch>7124479709",doc
?@ : AB
C Copyright Aahindra ?ritih Telecom "998
Training on C Programming Date : 20
th
-24
th
July
98
Topic : Advance-Pointer Page !o : 2 o# $
main. /
4
int diplay. /*
print#.DEn Addre o# #unction i FuG5 diplay /*
diplay./*
6
diplay. /
4
print#.DHunction name i diplayEnG/*
6
The ame #unction diplay./ can )e invo=ed uing pointer to #unction a #ollo3 :
main . /
4
int diplay./*
int .%#unc>ptr/./*
#unc>ptr0diplay*1% aign addre o# #unction %1
print#.DEnAddre o# #unction diplay i FuG5#unc>ptr/*
.%#unc>ptr/./* 1% invo=e the #unction diplay %1
6
int diplay./
4
print#.DHunction name i diplayEnG/*
6
Iere #unc>ptr i a pointer to a #unction5 3hich return an int ,
#unc>ptr i aigned an addre o# the #unction diplay./ ,
Ience to invo=e the #unction 3e &ut re;uire to 3rite the tatement :
.%#unc>ptr/. /
&#nction 'et#rnin( A Pointer
The 3ay #unction return an int5 char5 #loat 5 it can even return a pointer ,
main./
4
char %tr*
char %copy./*
char ource'(0GA?TG*
char target'"0(*
tr 0 copy.target5 ource/*
print#.DEnFG5tr/*
6
char %copy . char %t5 char %/
4
char %r*
r 0 t*
3hile.% J0 KE0L /
4
%t 0 % *
1var13331app1converion1tmp1cratch>7124479709",doc
?@ : AB
C Copyright Aahindra ?ritih Telecom "998
Training on C Programming Date : 20
th
-24
th
July
98
Topic : Advance-Pointer Page !o : $ o# $
t22*
22*
6
%t 0 KE0L*
return r*
6
)emory allocation
Mhenever a pointer i ued 5 it i important to allocate a memory to it ,
The #unction malloc./ i ued to allocate the memory to pointer o# ant type ,
void %malloc.i-e>t i-e/*
DmallocG return a pointer to pace #or an o)&ect o# i-e Di-eG or !NOO i# the re;uet can not )e
ati#ied ,The allocated pace i uninitiali-ed ,
e,g, int %ptr*
ptr 0 .int %/ malloc.i-eo#.int//*
The #unction
D#ree.addre/G #ree the pace pointed to )y addre,
e,g #ree.ptr/ #ree the pace pointed )y ptr , Mhere ptr i o)tained )y a call to malloc ,
There i no retriction on the order in 3hich pace i #reed5 )ut it i an error to #ree omething not
o)tained )y calling malloc ,
1var13331app1converion1tmp1cratch>7124479709",doc
?@ : AB
C Copyright Aahindra ?ritih Telecom "998

You might also like