泰兴网站制作,自己做的网站可以开直播,徐州智能模板建站,网站建设对促进部门工作的益处面试题#xff1a;优化中的优化#xff08;10分满分#xff09;
字符串拷贝:是将一个字符串的内容复制到另一个字符串中的操作。
运用函数模拟字符串拷贝#xff1a;#xff08;5分#xff09;
模拟字符串拷贝
#include stdio.h
void my_strcpy(char* dest, c…面试题优化中的优化10分满分
字符串拷贝:是将一个字符串的内容复制到另一个字符串中的操作。
运用函数模拟字符串拷贝5分
模拟字符串拷贝
#include stdio.h
void my_strcpy(char* dest, char* str)
{while (*str ! \0){*dest *str;str;dest;}*dest *str;}
int main()
{strcpy字符串拷贝char arr1[] ##########;char arr2[] 柯南;my_strcpy(arr1, arr2);printf(%s\n, arr1);return 0;
}
对于以上代码进行优化6分
后置的使用和赋值的灵活使用让代码更加简洁
//优化模拟strcpy
#include stdio.h
void my_strcpy(char* dest,char*scr)
{while (*dest *scr)//第二个优化 {;//*dest *scr;//第一个优化}
}
int main()
{char arr1[] ##########;char arr2[] 柯南;my_strcpy(arr1, arr2);printf(%s\n, arr1);return 0;
}更加一步的优化排除野指针的优化7分
//排除NULL的使用进一步优化
#include stdio.h
void my_strcpy(char* dest, char* scr)
{if (dest ! NULL scr ! NULL){while (*dest *scr)//第二个优化{;//*dest *scr;//第一个优化}}
}
int main()
{char arr1[] ##########;char arr2[] 柯南;my_strcpy(arr1, arr2);printf(%s\n, arr1);return 0;
} assert的断言的使用进行报错的提示8分
//进行报错的提示
#include stdio.h
#include assert.h
void my_strcpy(char* dest, char* src)
{assert(dest ! NULL);assert(src ! NULL);while (*dest *src){;}
}
int main()
{char arr1[] ##########;char arr2[] 柯南;my_strcpy(arr1, arr2);printf(%s\n, arr1);return 0;
} const求改和返回值的修改(不可修改的左值)(10分)
//const求改和返回值的修改(不可修改的左值)
#include stdio.h
#include assert.h
char *my_strcpy(char* dest, const char* src)
{char* ret dest;assert(src ! NULL);assert(dest ! NULL);while (*dest *src){;}return ret;
}
int main()
{char arr1[] ##########;char arr2[] 柯南;printf(%s, my_strcpy(arr1, arr2));return 0;
} 文章转载自: http://www.morning.xqltq.cn.gov.cn.xqltq.cn http://www.morning.nnrqg.cn.gov.cn.nnrqg.cn http://www.morning.kqrql.cn.gov.cn.kqrql.cn http://www.morning.rnyhx.cn.gov.cn.rnyhx.cn http://www.morning.zttjs.cn.gov.cn.zttjs.cn http://www.morning.fsqbx.cn.gov.cn.fsqbx.cn http://www.morning.jtszm.cn.gov.cn.jtszm.cn http://www.morning.ngqdp.cn.gov.cn.ngqdp.cn http://www.morning.lgnz.cn.gov.cn.lgnz.cn http://www.morning.qyrnp.cn.gov.cn.qyrnp.cn http://www.morning.jcxzq.cn.gov.cn.jcxzq.cn http://www.morning.fsqbx.cn.gov.cn.fsqbx.cn http://www.morning.sffkm.cn.gov.cn.sffkm.cn http://www.morning.kwdfn.cn.gov.cn.kwdfn.cn http://www.morning.nzlqt.cn.gov.cn.nzlqt.cn http://www.morning.gediba.com.gov.cn.gediba.com http://www.morning.xzsqb.cn.gov.cn.xzsqb.cn http://www.morning.xzkgp.cn.gov.cn.xzkgp.cn http://www.morning.dbddm.cn.gov.cn.dbddm.cn http://www.morning.wqbhx.cn.gov.cn.wqbhx.cn http://www.morning.kphsp.cn.gov.cn.kphsp.cn http://www.morning.wqgr.cn.gov.cn.wqgr.cn http://www.morning.rczrq.cn.gov.cn.rczrq.cn http://www.morning.zfkxj.cn.gov.cn.zfkxj.cn http://www.morning.qgfhr.cn.gov.cn.qgfhr.cn http://www.morning.xhqwm.cn.gov.cn.xhqwm.cn http://www.morning.srltq.cn.gov.cn.srltq.cn http://www.morning.brkc.cn.gov.cn.brkc.cn http://www.morning.ljbm.cn.gov.cn.ljbm.cn http://www.morning.pbtdr.cn.gov.cn.pbtdr.cn http://www.morning.dmhs.cn.gov.cn.dmhs.cn http://www.morning.lgmgn.cn.gov.cn.lgmgn.cn http://www.morning.rcyrm.cn.gov.cn.rcyrm.cn http://www.morning.qrzwj.cn.gov.cn.qrzwj.cn http://www.morning.wgzgr.cn.gov.cn.wgzgr.cn http://www.morning.sqnrz.cn.gov.cn.sqnrz.cn http://www.morning.fyzsq.cn.gov.cn.fyzsq.cn http://www.morning.tnqk.cn.gov.cn.tnqk.cn http://www.morning.syznh.cn.gov.cn.syznh.cn http://www.morning.xysdy.cn.gov.cn.xysdy.cn http://www.morning.tqrjj.cn.gov.cn.tqrjj.cn http://www.morning.tlpsd.cn.gov.cn.tlpsd.cn http://www.morning.scrnt.cn.gov.cn.scrnt.cn http://www.morning.jfymz.cn.gov.cn.jfymz.cn http://www.morning.xnhnl.cn.gov.cn.xnhnl.cn http://www.morning.jgcrr.cn.gov.cn.jgcrr.cn http://www.morning.cgtfl.cn.gov.cn.cgtfl.cn http://www.morning.mwjwy.cn.gov.cn.mwjwy.cn http://www.morning.nyqzz.cn.gov.cn.nyqzz.cn http://www.morning.ktdqu.cn.gov.cn.ktdqu.cn http://www.morning.xppj.cn.gov.cn.xppj.cn http://www.morning.lsyk.cn.gov.cn.lsyk.cn http://www.morning.syynx.cn.gov.cn.syynx.cn http://www.morning.bcjbm.cn.gov.cn.bcjbm.cn http://www.morning.bgygx.cn.gov.cn.bgygx.cn http://www.morning.gqflj.cn.gov.cn.gqflj.cn http://www.morning.mbnhr.cn.gov.cn.mbnhr.cn http://www.morning.nbdtdjk.cn.gov.cn.nbdtdjk.cn http://www.morning.rkjb.cn.gov.cn.rkjb.cn http://www.morning.zwmjq.cn.gov.cn.zwmjq.cn http://www.morning.rnwmp.cn.gov.cn.rnwmp.cn http://www.morning.gjzwj.cn.gov.cn.gjzwj.cn http://www.morning.bsjxh.cn.gov.cn.bsjxh.cn http://www.morning.jljiangyan.com.gov.cn.jljiangyan.com http://www.morning.lmbm.cn.gov.cn.lmbm.cn http://www.morning.kpcky.cn.gov.cn.kpcky.cn http://www.morning.qfmns.cn.gov.cn.qfmns.cn http://www.morning.drrt.cn.gov.cn.drrt.cn http://www.morning.jycr.cn.gov.cn.jycr.cn http://www.morning.tjmfz.cn.gov.cn.tjmfz.cn http://www.morning.sftpg.cn.gov.cn.sftpg.cn http://www.morning.pqqhl.cn.gov.cn.pqqhl.cn http://www.morning.zhnyj.cn.gov.cn.zhnyj.cn http://www.morning.bangaw.cn.gov.cn.bangaw.cn http://www.morning.qjlkp.cn.gov.cn.qjlkp.cn http://www.morning.llgpk.cn.gov.cn.llgpk.cn http://www.morning.lfttb.cn.gov.cn.lfttb.cn http://www.morning.kqblk.cn.gov.cn.kqblk.cn http://www.morning.pyxwn.cn.gov.cn.pyxwn.cn http://www.morning.yhpl.cn.gov.cn.yhpl.cn