资源描述:
《C函数、数组、指针和调试器gdb--字符串函数》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、C函数、数组、指针和调试器gdb—字符串函数TheClanguageprovidesmanyfunctionsthatareusedtoprocessstrings,andtheuseofthesefunctionscangreatlyreduceourworkload・One,putsandgets1,putsfunctionThefunctionoutputsastringterminatedin, 'tothescreen,forexample:Chara[]="Welcometo";Z/Char*p二〃"LinuxCPrograni";Puts(a);Puts(B);Willoutp
2、ut:WelcometoLinuxCProgram2,getsfunctionEnterastringfromtheterminalintothearrayofcharacters,whosereturnvalueisthefirstaddressofthearrayofcharacters,forexample:Charstring[20];Gets(string);//inputfromthekeyboardcomputerPuts(string);Thenoutput:ComputerTwo,strcpyandstrncpySincludeChar*strcpy(ch
3、ar,*dest,char,*src);Char^strncpy(char,*dest,char,*src,int,n);Explanation:(1)strcpyistheabbreviationofstringcopy.Whenusingthesetwofunctions,theheaderfilestring・hmustbeincluded・Thereturnvaluesofthesetwofunctionsareargumentsdest・(2)strcpycopiesthestring〃'"attheendof〃SRC〃tothearrayreferredtobydest・Str
4、ncpycopiesthefirstnbytesofthe' 'attheendofthestringpointedtobySRCtothearrayreferredtobydest・(3)thearraypointedtobyDeSTmustbelargeenoughtoholdthestringtowhichthecopiedSRCpoints・Thecopyiscopiedalongwiththestring's' '・(4)thefollowingcodeiswrong:Chara[]="Linux,C,Programz,,b[20];Youcannotassignastring,
5、constant,orarrayofcharactersdirectlytoanothercharacterarray.Stringreplicationcanonlyusefunctionssuchasstrcpy,strncpy,orothersimilarfunctions・(5)strcpyisnotsafe,therearesecurityvulnerabilities,whichareeasilyexploitedbyhackers・Therefore,strncpyshouldbeusedingenera1.Samplecode:Char*s二"HelloWorld";Char,
6、dl[20],d2[20];Strcpy(DI,s);Strncpy(D2,s,sizeof(s));Sizeof(s)getsthelengthofthestringtowhichtheSispointing,wheresizeof(s)isequalto12,becausethelast' 'ofthestring"HelloWorld"isalsoacharacter.Three,strcatandstrncat#includeCharEstreat(char,*dest,char,*src);Char^strncat(char,*dest,char,*sre,in
7、t,n);Explanation:(1)strcatistheabbreviationofstringcatenate(stringconnection).Theheaderfilesinthetwofunctionsareallstring,h,andthevalueoftheparameterdestisusedasthereturnvalue・(2)strcataddsthestringpo