You are on page 1of 11

Pointer and String

An array of characters terminated with a binary zero character (written as '\0') and represented in double quotes is called String.
Example:-

char name[]=HEASLER; Each character in the array occupies one byte o memory an! the last character is al"ays #$%&; #$%& is calle! #null 'haracter&; (ote that #$%&an! #%&is not same) AS'** +alue o #$%& is %, "hereas AS'** +alue o #%& is -.) /he character in memory "oul! loo0 li0e this: H A E S L E R $%

Example 1oi! main23 4 char name[]=Hello; int *; i=%; "hile2name[i]3 4 print 25$n 6c 6c 6c 6c,name[i],72name 8 i3,72* 8 name3,i[name]3; i88; 9 9 Outout is HHHH eeee
llll llll oooo

This program is one uses pointer to access the array elements. Another way is pointer contain the base address of the string.

Example:-

void main( ! char name" #$%Hello%& char 'ptr & ptr$name& (' )tore base address of string. '(

while('ptr *$ +,-. ! printf(/0c%1'ptr & ptr22& 3 3 4utput is Hello ptr is incremente! to point to the next character in the strin:) /his !eri+es rom t"o acets- array elements are store! in conti:uous memory locations an! on incrementin: a pointer it points to the imme!iately next location o its type) /his process is carrie! out till ptr !oesn&t point to the last character in the strin: that is #$%&)

Suppose "e "ish to store 5Hello) ;e may either store it in a strin: or "e may as0 the ' 'ompiler to store it at some location in memory an! assi:n the a!!ress o the strin: in a char pointer) /his is sho"n belo") char str[]=Hello; char 7p=Hello; /here is subtle !i erence in usa:e o these t"o orms) <or example, "e cannot assi:n a strin: to another, "hereas, "e can assi:n char pointer to another char pointer) /his is sho"n in the allo"in: pro:ram) =7 >ro:ram 7= +oi! main23 4 char str?[ ]=Hello; char str@[?%];

char 7s=Aoo! Bornin:; char 7C; str@=str?; C=s; 9 Also, once a strin: has been !e ine! it cannot be initialiDe! to another set o character) Enli0e Strin:, Such an Fperation is per ectly +ali! "ith char pointers) =7 >ro:ram 7= +oi! main23 4 char str[]=Hello; char 7p=Hello; str?=Gye; =7 Error 7= p=Gye; =7 ;or0s 7= 9 =7 Error 7= =7 ;or0s 7=

Standard Library String Functions' has a lar:e set o use ul strin: han!lin: library unctions) Here "e "oul! illustrate the usa:e o most commonly use! unctions (strlen(), strcpy(),strcat(),strcmp(),strlwr(),strupr(),strrev()) =7 strlen23 unction 7= int strin:len:th2char 73; +oi! main23 4 char 7arr; int len; print 25Enter the Strin: :- 3;

:ets2arr3; len=strin:len:th2arr3; print 25$nStrin: Len:th :- 6!,len3 :etch23; 9 int strin:len:th2char 7s3 4 int len:th=%; "hile27sH=&$%&3 4 len:th88; s88; 9 return 2len:th3; 9 Fut "oul! beI Enter the Strin: :- Hello Strin: Len:th :- J

/he unction strin:len:th23 is airly simple) *t 0eeps countin: the characters till en! o strin: is not met or in other "or!s 0eeps countin: character till the pointer s !oesn&t point to #$%&) Another unction that "e ha+e use! strcpy23) /his unction copies the contents o one strin: into another) /he base a!!resses o the source an! tar:et strin: shoul! be supplie! to the unction) =7 strcpy23 unction) 7= +oi! strin:copy2char 7t,char 7s3

4 "hile27s H= #$%&3 4 7t=7s; t88; s88; 9 7t=&$%&; 9 /he strcat23 unction concatenates the source strin: at the en! o the tar:et strin:) <or Example, 5Happy an! 5Home on concatenation "oul! result into a strin: 5HappyHome) =7 Strcat23 <unction 7= +oi! strin:concatenate2char 7s, char 7t3 4 "hile2 7tH = #$%&3 4 t88; 9 "hile27sH=&$%&3 4 7t88=7s88; 9 7t=&$%&; 9

The const Quali ier

/he Key"or! const 2 or 'onstant3, i present prece!es the !ata type o a +ariable) *t speci ies that the +alue o the +ariable "ill not chan:e throu:hout the pro:ram) Any attempt to alter the +alue o the +ariable !e ine! "ith this Cuali ier "ill result into an error messa:e rom compiler) 'onst is better i!ea as compare! to L!e ine because its scope o operation can be controlle! by placin: it appropriately either insi!e a unction or outsi!e all unctions) * a const is place! insi!e a unction its e ect "oul! be localiDe! to that unction, "hereas, i it is place! outsi!e all unction then its e ect "oul! be :lobal) ;e cannot exercise such iner control "hile usin: a L!e ine)

Loo0 at the allo"in: pro:ram: =7 >ro:ram 7= Lin+lu!eMst!io)hN Linclu!eMconio)hN +oi! xstrcpy2char 7,char 73; +oi! main23 4 char str?[ ]=(a:pur; char str@[?%%]; xstrcpy2str@,str?3; print 25$n 6s,str@3; 9 1oi! xstrcpy2char 7t,char 7s3 4 ;hile27tH=&$%& 4 7t=7s; t88;

s88; 9 7t=&$%&; 9 /his pro:ram simply copies the contents o str?[ ] into str@[ ] usin: the unction xstrcpy23) * "e a!! the allo"in: lines beyon! the last statement o xstrcpy23, /han s=s-O; 7s=&0&; /his "oul! chan:e the source strin: to 5Ka:pur) 'an "e not ensure that source !oesn&t chan:e e+en acci!entally in xstrcpy23P ;e can by chan:in: the prototype o the unction to +oi! xstrcpy2char 7,const char73; 'orrespon!in:ly the !e inition "oul! chan:e to: +oi! xstrcpy2char 7t, const char 7s3 4 ==co!e 9

/he allo"in: co!e ra:ement "oul! help you ix your i!eas about const urther) char7p = Hello; 7p = #B&; >=Gye; const char7p = Hello; 7p = #B&; >=Gye; char const 7p = Hello; =7pointer is +ariable, so is strin:7= =7;or0s7= =7;or0s7= =7Strin: is constant pointer is not7= =7error7= =7;or0s7= =7Strin: is constant pointer is not7=

7p = #B&; >=Gye; char 7const p = Hello; 7p = #B&; >=Gye;

=7error7= =7;or0s7= =7Strin: is constant pointer is not7= =7"or0s7= =7error7= =7Strin: is constant pointer is not7=

const char const 7p = Hello; 7p = #B&; >=Gye;

=7error7= =7error7=

!rray o Pointer to string


AS "e 0no" that , a pointer +ariable al"ays contain an a!!ress) /her oe , i "e contruct an array o pointer it "oul! contain a number o a!!resses) Example is:-

char 7n[]=4 5Abc, 5>ara:, 5Raman, 5Aopal, 5RaQesh, 5Srini+as 9;

*n /his !eclaration n[ ]is an array o pointers)it contain base a!!ress o respecti+e name) /hat is base a!!ress o 5Abc is store! in n[%],base a!!ress o 5>ara: is store! in n[?],an! so on)

A!!ress ?%%?%-. @%%O -%%. O%%@ S??.

n[] n[%] n[?] n[@] n[R] n[-] n[J]

Strin: 5Abc$% 5Raman$% 5Aopal$% 5RaQesh$% 5>ara:$% 5Srini+as$%

n[ ] ?%%.??@ ?%-. .??@%%O .??O -%%. .??. O%%@ .?@% S??. .?@@

/hus one reason to store strin: in an array o pointers is to ma0e more e icient use o a+ailable memory) Another reason to use array o pointer to store strin: is to obtain :reater ease in the manipulation o the strin:s) /he allo"in: pro:ram sho"s this)

+oi! main23 4 char 7n[ ]=4 5Abc, 5>ara:, 5Raman, 5Aopal, 5RaQesh, 5Srini+as

char 7temp; print 25$n Fri:inal : 6s 6s,n[@],n[R]3; temp=n[@]; n[@]=n[R]; n[R]=temp; print 25$n (e" : 6s 6s,n[@],n[R]3; :etch23; 9

Futput is Fri:inal : Raman Aopal (e" : Aopal Raman * ;e use unction than "e allo" these concept:Swap(char ""s#,char ""s$) % t&"s#' "s#&"s$' "s$&t' (

Pass $-) array to unction*Example:Linclu!eMst!io)hN Linclu!eMconio)hN

+oi! unction2char 7temp3 4 print 256s $n,temp3; 9 +oi! main23 4 int *; char 7temp[]; print 25$n$n Ho" many (ames !o you "ant to enter :3; scan 256!,Tn3; or2i=%;iMn;i883 4 print 25$n$n eneter (ame -N 6! :,i8?3; lushall23; :ets2temp[i]3; 9 or2i=%;iMn;i883 4 unction2temp[i]3; 9 :etch23; 9

You might also like