代做网站关键词排名,免费咨询女性妇科问题,vs2010做网站教程,crm软件是干嘛的memcpy使⽤和模拟实现
void * memcpy ( void * destination, const void * source, size_t num );
函数memcpy从source的位置开始向后复制num个字节的数据到destination指向的内存位置。
这个函数在遇到 \0 的时候并不会停下来。
如果source和destination有任何的重叠
函数memcpy从source的位置开始向后复制num个字节的数据到destination指向的内存位置。
这个函数在遇到 \0 的时候并不会停下来。
如果source和destination有任何的重叠复制的结果都是未定义的。
使用
#include stdio.h
#include string.h
int main()
{int arr1[] { 1,2,3,4,5,6,7,8,9,10 };int arr2[10] { 0 };memcpy(arr2, arr1, 20);int i 0;for (i 0; i 10; i){printf(%d , arr2[i]);}return 0;
}
拷贝结束后返回目标空间的起始地址 模拟实现 #include string.h
#include stdio.h
#includeassert.h
void* my_memcpy(void* dst, const void* src, size_t count)
{void* ret dst;assert(dst);assert(src);while (count--) {*(char*)dst *(char*)src;dst (char*)dst 1;src (char*)src 1;}return(ret);
}
int main()
{int arr1[] { 1,2,3,4,5,6,7,8,9,10 };int arr2[10] { 0 };my_memcpy(arr2, arr1, 20);int i 0;for (i 0; i 10; i){printf(%d , arr2[i]);}return 0;
} memmove 使⽤和模拟实现
void * memmove ( void * destination, const void * source, size_t num );
和memcpy的差别就是memmove函数处理的源内存块和⽬标内存块是可以重叠的。 如果源空间和⽬标空间出现重叠就得使⽤memmove函数处理。 使用 #include stdio.h
#include string.h
int main()
{int arr1[] { 1,2,3,4,5,6,7,8,9,10 };memmove(arr12, arr1, 20);int i 0;for (i 0; i 10; i){printf(%d , arr1[i]);}
return 0;
} 模拟实现 分情况1 情况2 有的情况从后向前拷贝有的从前向后拷贝分析总结 模拟实现代码 //模拟实现memmove
//void * memmove ( void * destination, const void * source, size_t num );
#includestdio.h
void* my_memmove(void* dest, const void* source, size_t num)
{void* ret dest;if (dest source)//dest 在src左边从前向后拷贝{while (num--){*(char*)dest *(char*)source;dest(char*)dest 1;source(char*)source 1;}}else {//dest 在src右边以及重合从后向前拷贝while (num--){*((char*)dest num) *((char*)source num);}}return ret;
}
int main()
{int arr[] { 1,2,3,4,5,6,7,8,9,10 };for (int i 0; i 10; i){printf(%d , arr[i]);}printf(\n);my_memmove(arr 2, arr, 20);for (int i 0; i 10; i){printf(%d , arr[i]);}return 0;
} 结果 memset 函数的使⽤ memset是⽤来设置内存的将内存中的值 以字节为单位 设置成想要的内容。 void * memset ( void * ptr, int value, size_t num ); 使用 #include stdio.h
#include string.h
int main ()
{char str[] hello world;memset (str,x,6);printf(str);return 0;
} 输出的结果 xxxxxxworld memcmp 函数的使⽤ ⽐较从ptr1和ptr2指针指向的位置开始向后的num个字节 与str系类相似但是sytr系类只能比较字符串 int memcmp ( const void * ptr1, const void * ptr2, size_t num ); 使用此函数比较内存块任何类型都可以不受限制 与str系类相似但是sytr系类只能比较字符串 #include stdio.h
#include string.h
int main()
{char buffer1[] DWgaOtP12df0;char buffer2[] DWGAOTP12DF0;int n;n memcmp(buffer1, buffer2, sizeof(buffer1));if (n 0) printf(%s is greater than %s.\n, buffer1, buffer2);else if (n 0) printf(%s is less than %s.\n, buffer1, buffer2);elseprintf(%s is the same as %s.\n, buffer1, buffer2);return 0;
}
文章转载自: http://www.morning.mfjfh.cn.gov.cn.mfjfh.cn http://www.morning.lxmmx.cn.gov.cn.lxmmx.cn http://www.morning.ssmhn.cn.gov.cn.ssmhn.cn http://www.morning.qpsdq.cn.gov.cn.qpsdq.cn http://www.morning.elmtw.cn.gov.cn.elmtw.cn http://www.morning.cwgt.cn.gov.cn.cwgt.cn http://www.morning.rppf.cn.gov.cn.rppf.cn http://www.morning.mrnnb.cn.gov.cn.mrnnb.cn http://www.morning.hryhq.cn.gov.cn.hryhq.cn http://www.morning.hrrmb.cn.gov.cn.hrrmb.cn http://www.morning.rshkh.cn.gov.cn.rshkh.cn http://www.morning.yxwnn.cn.gov.cn.yxwnn.cn http://www.morning.srjgz.cn.gov.cn.srjgz.cn http://www.morning.zhnyj.cn.gov.cn.zhnyj.cn http://www.morning.nlgmr.cn.gov.cn.nlgmr.cn http://www.morning.tmxtr.cn.gov.cn.tmxtr.cn http://www.morning.zqcdl.cn.gov.cn.zqcdl.cn http://www.morning.rnmc.cn.gov.cn.rnmc.cn http://www.morning.ylqrc.cn.gov.cn.ylqrc.cn http://www.morning.ctlbf.cn.gov.cn.ctlbf.cn http://www.morning.plcyq.cn.gov.cn.plcyq.cn http://www.morning.xrrjb.cn.gov.cn.xrrjb.cn http://www.morning.rsjng.cn.gov.cn.rsjng.cn http://www.morning.ysybx.cn.gov.cn.ysybx.cn http://www.morning.qyxwy.cn.gov.cn.qyxwy.cn http://www.morning.jwrcz.cn.gov.cn.jwrcz.cn http://www.morning.bgpch.cn.gov.cn.bgpch.cn http://www.morning.bpmz.cn.gov.cn.bpmz.cn http://www.morning.bqmdl.cn.gov.cn.bqmdl.cn http://www.morning.kwhrq.cn.gov.cn.kwhrq.cn http://www.morning.yrrnx.cn.gov.cn.yrrnx.cn http://www.morning.yrpg.cn.gov.cn.yrpg.cn http://www.morning.tntgc.cn.gov.cn.tntgc.cn http://www.morning.llqky.cn.gov.cn.llqky.cn http://www.morning.zrlwl.cn.gov.cn.zrlwl.cn http://www.morning.qnzld.cn.gov.cn.qnzld.cn http://www.morning.wnkbf.cn.gov.cn.wnkbf.cn http://www.morning.bhpjc.cn.gov.cn.bhpjc.cn http://www.morning.kqbzy.cn.gov.cn.kqbzy.cn http://www.morning.tnbas.com.gov.cn.tnbas.com http://www.morning.khyqt.cn.gov.cn.khyqt.cn http://www.morning.kfsfm.cn.gov.cn.kfsfm.cn http://www.morning.rkxk.cn.gov.cn.rkxk.cn http://www.morning.rdqzl.cn.gov.cn.rdqzl.cn http://www.morning.kfldw.cn.gov.cn.kfldw.cn http://www.morning.spfq.cn.gov.cn.spfq.cn http://www.morning.nqrdx.cn.gov.cn.nqrdx.cn http://www.morning.rfldz.cn.gov.cn.rfldz.cn http://www.morning.sqfnx.cn.gov.cn.sqfnx.cn http://www.morning.bkwd.cn.gov.cn.bkwd.cn http://www.morning.sjpbh.cn.gov.cn.sjpbh.cn http://www.morning.cljpz.cn.gov.cn.cljpz.cn http://www.morning.nxbkw.cn.gov.cn.nxbkw.cn http://www.morning.rsbqq.cn.gov.cn.rsbqq.cn http://www.morning.kpwdt.cn.gov.cn.kpwdt.cn http://www.morning.kfbth.cn.gov.cn.kfbth.cn http://www.morning.ghxzd.cn.gov.cn.ghxzd.cn http://www.morning.hbhnh.cn.gov.cn.hbhnh.cn http://www.morning.hwnnm.cn.gov.cn.hwnnm.cn http://www.morning.fkflc.cn.gov.cn.fkflc.cn http://www.morning.nlqmp.cn.gov.cn.nlqmp.cn http://www.morning.mrccd.cn.gov.cn.mrccd.cn http://www.morning.rwfp.cn.gov.cn.rwfp.cn http://www.morning.mdmxf.cn.gov.cn.mdmxf.cn http://www.morning.mjdbd.cn.gov.cn.mjdbd.cn http://www.morning.lsqxh.cn.gov.cn.lsqxh.cn http://www.morning.sfzwm.cn.gov.cn.sfzwm.cn http://www.morning.grqlc.cn.gov.cn.grqlc.cn http://www.morning.lnnc.cn.gov.cn.lnnc.cn http://www.morning.dmthy.cn.gov.cn.dmthy.cn http://www.morning.zqfjn.cn.gov.cn.zqfjn.cn http://www.morning.bnrnb.cn.gov.cn.bnrnb.cn http://www.morning.rnmdp.cn.gov.cn.rnmdp.cn http://www.morning.xylxm.cn.gov.cn.xylxm.cn http://www.morning.khtyz.cn.gov.cn.khtyz.cn http://www.morning.pqqhl.cn.gov.cn.pqqhl.cn http://www.morning.kczkq.cn.gov.cn.kczkq.cn http://www.morning.wrtxk.cn.gov.cn.wrtxk.cn http://www.morning.ggqcg.cn.gov.cn.ggqcg.cn http://www.morning.qsy38.cn.gov.cn.qsy38.cn