字符串经典案例之一(字符串的copy)
来源:互联网 发布:微商渠道网络推广 编辑:程序博客网 时间:2024/05/22 05:28
1、初级程序
int main04(){ char a[] = "i am a student"; char b[64]; int i = 0; for(int i = 0; *(a+i) !='\0'; i++) { *(b+i) = *(a+i); } //'\0'并没有copy到b中 b[i] = '\0';//重要 printf("a:%s \n",a); printf("b:%s \n",b); printf("Hello world!\n"); system("pause"); return 0;}
2、接口的封装及优化
2.1 方法一
//定义接口//方法一void copy_str21(char *from, char *to){ for(; *from != '\0'; from++, to++) { *to = *from; } *to = '\0';//重要 手工补‘\0’ return;}
2.2 方法二
//方法二 优化void copy_str22(char *from, char *to){ //++的优先级更高 for(;*from != '\0';) { *to++ = *from++;//相当于 先*to = *from 再from++ to** } *to = '\0';//重要 手工补‘\0’ return;}
2.3 方法三
//方法三 优化void copy_str23(char *from, char *to){ while((*to = *from) != '\0')//字符copy后不为0 { from ++; to ++; } //循环到最后‘\0’已经加到to里面,所以不需要手工加'\0'}
2.4 方法四
//方法四 优化void copy_str24(char *from, char *to){ while((*to++ = *from++) != '\0') { ; }}
2.5 方法五
//方法五 优化void copy_str25(char *from, char *to){ while((*to++ = *from++)) { ; }}
2.5_、方法五的引申
void copy_str25_error(char *from, char *to){ while((*to++ = *from++)) { ; } printf("from:%s \n",from);//此时from指向字符串的最后位置“\0”,所以打印不出字符串}
2.6、方法六 辅助指针变量的重要性及错误检测
//方法六 优化//辅助指针变量 不轻易改变形参的值,用辅助变量把形参接过来int copy_str26(char *from, char *to){ char *tmpfrom = from; char *tmpto = to; if(tmpfrom == NULL || tmpto == NULL) { return -1; } while(*tmpto++ = *tmpfrom++); printf("from:%s \n",from);}
main 函数一
int main(){ char *from = "abcdefg"; char buf2[100];// copy_str21(from,buf2);// copy_str22(from,buf2);// copy_str23(from,buf2);// copy_str24(from,buf2); copy_str25(from,buf2); printf("from:%s \n",from); printf("buf2:%s \n",buf2); printf("Hello world!\n"); system("pause"); return 0;}
main函数二 初始化指针最好分配内存
//错误案例int main06(){ char *from = "abcdefg"; char buf2[100]; int ret = 0; //错误案例// {//// //相当于*(0) = 'a' 不能修改特殊空间// char *myto = NULL;// 因此必须分配内存空间 没有内存就没有指针// copy_str21(from,myto);// error// } { char *myto = NULL;//必须分配内存 ret = copy_str26(from,myto); if(ret != 0) { printf("func copy_str26 error:%d \n",ret); return ret; } } printf("Hello world!\n"); system("pause"); return ret;}
main函数三 赋值指针的重要性
//引入辅助指针变量的重要性int main(){ char *from = "abcdefg"; char buf2[100]; int ret = 0; printf("copy_str25_error begin!\n"); //copy_str25_error(from,buf2);// 不能打印任何值 copy_str26(from,buf2); printf("copy_str25_error end! \n"); printf("Hello world!\n"); system("pause"); return ret;}
阅读全文
0 0
- 字符串经典案例之一(字符串的copy)
- copy的字符串处理;
- c 语言字符串的copy
- 关于字符串的copy函数
- copy:字符串copy程序、泛型编程的copy
- C语言的字符串(篇章之一)
- 经典的字符串问题
- 经典的字符串hash
- 字符和字符串的输入输出(经典)
- 字符串copy函数的技术推演过程
- 序列之一 字符串 (python)
- 字符串copy函数
- 字符串的经典hash算法
- 字符串的经典hash算法
- 字符串的经典hash算法
- 字符串的经典hash算法
- 经典的字符串hash函数。
- 字符串的经典hash算法
- 图像去雨算法(基于卷积网络)
- 关于添加pdf水印那些事
- ftp上传多个文件脚本
- 技术问题
- Java菜鸟编程 一个有趣的练习
- 字符串经典案例之一(字符串的copy)
- 环境搭建及系统部署
- tp框架中的多表查询操作
- 使用OkHttp之Websocket实现长连接
- Java设计模式--工厂模式
- MySql 5.7.13 for Mac 密码重置教程
- 【UGUI】Unity的UGUI实现Button按钮长按状态的判断
- Java安全学习笔记(十一)-RSA算法加密和解密
- 关于android日志工具