指针变量与其指向内存的关系 指针变量也是一种变量,占有内存空间,用来保存内存地址测试指针变量占有内存空间大小。defineCRTSECURENOWARNINGSincludestdio。hincludestdlib。hincludestring。hincludetime。hintmain0101(){charpNULL;charbuf〔〕abcde;printf(p1d,p);改变指针变量的值pbuf;printf(p2d,p);指针变量和它指向的内存块是两个不同的概念pp1;改变指针变量的值,即改变了指针的指向printf(p3d,p);printf(bufs,buf);printf(pc,p);bprintf(改变指针指向的内存,并不会改变指针的值);buf〔1〕1;printf(p4d,p);printf(buf2s,buf);pm;printf(p5d,p);printf(buf3s,buf);写内存时,一定要确保内存可写charbuf2aaawwweee;该字符串在文字常量区不可修改buf2〔2〕1;errcharbuf3〔〕wwweerrr;buf3〔1〕s;ok不允许向NULL和未知非法地址拷贝内存。charp3NULL;errcharp30x1111;err给p3指向的内存中拷贝字符串p3buf3;okstrcpy(p3,123);return0;}修改指针变量的结果defineCRTSECURENOWARNINGSincludestdio。hincludestdlib。hincludestring。hincludetime。hintmain0201(){charpNULL;charqNULL;charbuf〔100〕asdzcx;pbuf〔0〕;printf(pd,c,p,p);pbuf〔1〕;printf(p2d,c,p,p);printf();for(inti0;istrlen(buf);i){pbuf〔i〕;printf(p3d,c,p,p);}q(char)malloc(100);if(qNULL){return1;}strcpy(q,qqqwww);for(inti0;istrlen(q);i){pqi;printf(c,p);}return0;}3。通过指针间接复赋值defineCRTSECURENOWARNINGSincludestdio。hincludestdlib。hincludestring。hincludetime。hintgeta(){inta10;returna;}voidgeta2(intb){b20;}voidgeta3(intp){p20;通过操作内存}voidgeta4(inta1,inta2,inta3,inta4){a11;a22;a33;a44;}intmain0301(){inta100;intpNULL;建立关系指针指向谁,就将谁的地址赋值给指针pa;通过操作内存p22;通过指针间接赋值1。两个变量2。建立关系3。通过操作内存intbgeta();printf(bd,b);geta2(b);printf(b2d,b);如果想通过形参改变实参的值,必须地址传递geta3(b);函数调用时建立关系printf(b3d,b);inta1,a2,a3,a4;geta4(a1,a2,a3,a4);printf(a1d,a2d,a3d,a4d,a1,a2,a3,a4);return0;}staticvoidfun2(intp){p0xaabb;printf(fun2:pp,p);}staticvoidfun3(intp){p0xaabb;printf(fun3:pp,p);}intmain0302(){一个变量,应该定义一个怎样类型的指针来保存它的地址在原来的基础上加一个inta10;intpa;intqp;inttNULL;intt2t;intp0x1122;printf(p1p,p);fun2(p);值传递printf(p2p,p);fun3(p);值传递printf(p3p,p);return0;}4。指针作为函数参数的输入输出特性defineCRTSECURENOWARNINGSincludestdio。hincludestdlib。hincludestring。hincludetime。hintgeta(){inta10;returna;}voidgeta2(intb){b20;}voidgeta3(intp){p20;通过操作内存}voidgeta4(inta1,inta2,inta3,inta4){a11;a22;a33;a44;}intmain0301(){inta100;intpNULL;建立关系指针指向谁,就将谁的地址赋值给指针pa;通过操作内存p22;通过指针间接赋值1。两个变量2。建立关系3。通过操作内存intbgeta();printf(bd,b);geta2(b);printf(b2d,b);如果想通过形参改变实参的值,必须地址传递geta3(b);函数调用时建立关系printf(b3d,b);inta1,a2,a3,a4;geta4(a1,a2,a3,a4);printf(a1d,a2d,a3d,a4d,a1,a2,a3,a4);return0;}staticvoidfun2(intp){p0xaabb;printf(fun2:pp,p);}staticvoidfun3(intp){p0xaabb;printf(fun3:pp,p);}intmain0302(){一个变量,应该定义一个怎样类型的指针来保存它的地址在原来的基础上加一个inta10;intpa;intqp;inttNULL;intt2t;intp0x1122;printf(p1p,p);fun2(p);值传递printf(p2p,p);fun3(p);值传递printf(p3p,p);return0;}5。字符串初始化defineCRTSECURENOWARNINGSincludestdio。hincludestdlib。hincludestring。hincludetime。hC语言没有字符串类型,而是通过字符数组模拟C语言字符串以字符即数字0结尾intmain0501(){不指定长度,没有结束符0,有多少个元素就有多长charbuf〔〕{a,b,c};3个元素printf(bufs,buf);不加的话末尾乱码指定长度,后面没有赋值的元素位置,自动补0charbuf1〔100〕{a,b,c};printf(buf2s,buf1);所谓元素都赋值为0charbuf3〔100〕{0};charbuf4〔2〕{1,2,3};err数组越界charbuf5〔50〕{1,a,b,0,7};printf(buf5s,buf5);charbuf6〔50〕{1,a,b,0,7};printf(buf6s,buf6);1abcharbuf7〔50〕{1,a,b,,7};printf(buf7s,buf7);1ab使用字符串初始化,常用此类方式charbuf8〔〕qaaasss;strlen:测字符串长度,但不包含数字0与字符sizeof:测数组长度,包含数字0和字符printf(strlend,sizeofd,strlen(buf8),sizeof(buf8));78charbuf9〔100〕qaaasss;printf(strlend,sizeofd,strlen(buf9),sizeof(buf9));7100printf(test);12相当于charstr〔〕129;printf(s,str);return0;}intmain0502(){charbuf〔〕aaazzzzssssdddd;charpNULL;〔〕方式for(inti0;istrlen(buf);i){printf(c,buf〔i〕);}printf();指针法数组名字,是数组首元素地址pbuf;for(inti0;istrlen(buf);i){printf(c,p〔i〕);}printf();for(inti0;istrlen(buf);i){printf(c,(pi));编译器方式}printf();for(inti0;istrlen(buf);i){printf(c,(bufi));}printf();buf和p完全等价吗p;okbuf;errbuf只是一个常量,不可修改return0;}6。字符串拷贝defineCRTSECURENOWARNINGSincludestdio。hincludestdlib。hincludestring。hincludetime。hintmain0601(){charsrc〔〕qqqqqqqqqq;chardst〔100〕{0};inti0;for(;src〔i〕!0;i){dst〔i〕src〔i〕;}补齐结束符dst〔i〕0;printf(s,dst);return0;}voidmystrcpy(chardst,charsrc){inti0;for(;(srci)!0;i){(dsti)(srci);dst〔i〕src〔i〕}dst〔i〕0;(dsti)0;}voidmystrcpy2(chardst,charsrc){while(src!0){dstsrc;src;dst;}dst0;}voidmystrcpy3(chardst,charsrc){dstsrcdst,src判断dst是否为0,为0跳出循环while(dstsrc){NULL;先执行,再自加;先dstsrc,dst,src}}成功返回0,失败返回非零1。判断形参指针是否为NULL2。不要直接使用形参以防止将形参的指针指向末尾intmystrcpy4(chardst,charsrc){if(dstNULLsrcNULL){return1;}使用辅助变量将形参的值接来chartodst;charfromsrc;dstsrcdst,src判断dst是否为0,为0跳出循环while(fromto){NULL;先执行,再自加;先dstsrc,dst,src}printf(mystrcpy4:dsts,dst);return0;}intmai0602n(){charsrc〔〕qqqqqqqqqq;chardst〔100〕{0};intret0;retmystrcpy4(dst,src);if(ret!0){printf(mystrcpy4err:d,ret);returnret;}printf(s,dst);inti0;intni;printf(nd,id,n,i);return0;}7。strstr中的while与dowhile的模型defineCRTSECURENOWARNINGSincludestdio。hincludestdlib。hincludestring。hincludetime。hintmain01(){charpsadzxaaadaszxaaaasszxaa231312zxaa4;zxaaintn0;do{pstrstr(p,zxaa);if(p!NULL){n;累计个数重新设置查找起点ppstrlen(zxaa);}else若没有匹配的字符串跳出循环{break;}}while(p!0);如果没到结尾printf(nd,n);return0;}intmain02(){charpsadzxaaadaszxaaaasszxaa231312zxaa4;zxaaintn0;while((pstrstr(p,zxaa))!NULL){能进循环一定有匹配到子串重新设置起点位置ppstrlen(zxaa);n;if(p0)如果到结束符{break;}}printf(nd,n);return0;}intmystrstr(charp,intn){两个辅助变量inti0;chartempp;while((tempstrstr(temp,zxaa))!NULL){能进循环一定有匹配到子串重新设置起点位置temptempstrlen(zxaa);i;if(temp0)如果到结束符{break;}}ni;return0;}intmain07(){charpsadzxaaadaszxaaaasszxaa231312zxaa4;zxaaintn0;intret0;retmystrstr(p,n);if(ret!0){returnret;}printf(nd,n);return0;}