《(7.4)--7.4Pointerandstring.pdf》由会员分享,可在线阅读,更多相关《(7.4)--7.4Pointerandstring.pdf(9页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、Pointer and stringPointer and string7.4Each character in a string(including theending 0)is stored in sequence in thememory.If a character pointer points to thismemory,it can be used to manipulate thestring.Pointer and string7.4psHello0String constantpHello0sEgchar s=Hello;char*ps=s;char*p=Hello;Poin
2、ter and string7.4When using cin to input a string,the input from keyboard is actually stored in the memory pointed to by a pointer;when using cout to output a string,the characters in the memory pointed to by the pointer are actually taken out one by one and output to the screen until the end of 0.P
3、ointer and string7.4char s=Hello;char*p=Hello;char*ps=s;cinps;/Assume that the input is“abc”coutp;coutp+2;psabc0o0String constantpHello0s+2EgPointer and string7.4Eg1Write a program to implement the operation of taking a substring from a given string.Take a substring started from the 4thcharacter in
4、the string my book!,and take 4 characters in all togenerate a new string.Pointer and string7.4int main()char s=my book!;char t5;int i,start=3,len=4;char*p=s+start;for(i=0;i len;i+)ti=pi;/Equivalent to*(t+i)=*(p+i)ti=0;coutThe substring is:tendl;return 0;myboosk!0tPointer and string7.4Eg2Manipulation of multiple strings using array of pointers.int main()char*p3=Beijing,Tianjin,Shanghai;int i;for(i=0;i 3;i+)coutpiendl;return 0;Pointer and string7.4pP0p1P2Shanghai0Tiangin0Beijing0