当前位置: 首页 > news >正文

保山网站建设服务网站排名seo培训

保山网站建设服务,网站排名seo培训,海南省人民政府网站,宿迁网站建设strcpy拷贝的仅仅是字符串,但是内存中的数据不仅仅是字符,所以就有了memcpy函数 1. memcpy void *memcpy (void * destination ,const void * source , size_t num) 函数memcpy从source的位置开始向后拷贝num个字节的数据到desti…

 strcpy拷贝的仅仅是字符串,但是内存中的数据不仅仅是字符,所以就有了memcpy函数

1. memcpy

void *memcpy (void * destination ,const void * source , size_t num)

函数memcpy从source的位置开始向后拷贝num个字节的数据到destination的内存位置

#include<string.h>
int main()
{int arr1[10] = { 0 };int arr2[] = { 1,2,3,4,5 };//把arr2中的前5个整形的数据,拷贝放到arr1中memcpy(arr1, arr2, 20);return 0;
}

可以看到前5个整形都拷贝过来了 


 memcpy函数的模拟实现:

(1)一个字节一个字节的拷贝(dest,src转为char *

eg:如果拷贝7个字节(两个int *类型指针不能操作)

(2)(char *)dest++不能这样写,因为++的优先级高于强制类型转换

相当于先对原类型进行++,再进行进行强转

前置++(char *)dest虽然C语言中可以,但是改为c++就不能运行,所以还是正常写+1

#include<string.h>
#include<assert.h>
void* my_memcpy(void* dest, const void* src, size_t sz)
{void * ret=dest;assert(dest && src);while (sz--){*(char*)dest = *(char*)src;dest = (char*)dest + 1;src = (char*)src + 1;}return ret;
}

一个数组arr={1,2,3,4,5,6,7,8,9,10},如果想在自身的基础上进行拷贝,即把1,2,3,4,5拷贝到3,4,5,6,7的位置上,想得到结果:1,2,1,2,3,4,5,8,9,10

但是得到的结果却是1 2 1 2 1 2 1 8 9 10

我们就发现不重叠内存的拷贝,可以使用memcpy,

重叠内存的拷贝,使用memmove函数

 上面其实是标准规定,但是实际在VS2022这个环境中,memcpy也能实现重叠内存的拷贝

(其他平台不一定)

#include<stdio.h>
#include<string.h>
#include<assert.h>
void* my_memcpy(void* dest, const void* src, size_t sz)
{assert(dest && src);while (sz--){*(char*)dest = *(char*)src;dest = (char*)dest + 1;//这里不能写成(char*)dest++,强制类型转化是临时的,++的时候dest并不一定是char*类型的src = (char*)src + 1;}
}
int main()
{int arr[] = { 1,2,3,4,5,6,7,8,9,10 };my_memcpy(arr + 2, arr, 20);for (int i = 0; i < 10; i++){printf("%d ", arr[i]);}return 0;
}

 2. memmove

void *memmove(void * destination ,const void * source , size_t num)

 参数和memcpy一样,但是memmove可以实现重叠内存的拷贝


 memmove函数的模拟实现:

(这里不考虑直接开辟一个相同的空间再进行拷贝元素)

讨论:

dest<src:只能从前向后拷贝

dest>src:只能从后向前拷贝

dest和src不相干的时候:从后往前或者从前往后都可以

#include<stdio.h>
#include<assert.h>
void* my_memmove(void* dest, const void* src, size_t sz)
{void* ret = dest;assert(dest && src);if (dest < src){//前->后while (sz--){*(char*)dest = *(char*)src;dest = (char*)dest+1;src = (char*)src+1;}}else{//后->前while (sz--){*((char*)dest + sz) = *((char*)src + sz);}}return ret;
}
int main()
{int arr[20] = { 1,2,3,4,5,6,7,8,9,10 };my_memmove(arr , arr+2, 20);for (int i = 0; i < 10; i++){printf("%d ", arr[i]);}return 0;
}

3. memcmp

int memcmp ( const void * ptr1,const void *( ptr2  ,  size_t num )

 类似strcmp函数,memcmp是一对字节一对字节进行比较,比较num个字节ACSII值

(1)返回值:

  • 如果返回值 < 0,则表示 str1 小于 str2
  • 如果返回值 > 0,则表示 str1 大于 str2
  • 如果返回值 = 0,则表示 str1 等于 str2
#include<stdio.h>
#include<string.h>
int main()
{int arr1[] = { 1,2,3,4,5,6,7 };//01 00 00 00 02 00 00 00 03 00 00 00 04 00 00 00 05 00 00 00 06 00 00 00 07 00 00 00int arr2[] = { 1,2,3,0x11223304 };//01 00 00 00 02 00 00 00 03 00 00 00 04 33 22 11 int ret = memcmp(arr1, arr2, 13);printf("%d\n", ret);
}

上面这个代码虽然arr1的元素多,但是比较前13个字节的大小都一样,那么ret=0


4. memset

void *memset( void *dest, int c, size_t count

复制字符c(一个无符号字符)到参数str所指向的字符串的前n个字符

(初始化前count个字节为c)

是以字节为单位设置内存的

eg1:将hello world中的wor改为xxx:

#include<stdio.h>
#include<string.h>
int main()
{char arr[] = "hell0 world";memset(arr + 6, 'x', 3);printf("%s\n", arr);return 0;
}

eg2: 思考这个代码将arr改为了啥?

#include<stdio.h>
#include<string.h>
int main()
{int arr[10] = { 0 };memset(arr, 1, 40);return 0;
}

每一个int类型的元素的每一个字节都改为了1

所以想把数组每一个元素都初始化为1,用memset函数是不可能实现的

但是可以都初始化为0


本次内容就到此啦,欢迎评论区或者私信交流,觉得笔者写的还可以,或者自己有些许收获的,麻烦铁汁们动动小手,给俺来个一键三连,万分感谢 ! 


文章转载自:
http://dinncoexpiable.bpmz.cn
http://dinncomorphoneme.bpmz.cn
http://dinncomortgage.bpmz.cn
http://dinncohabenula.bpmz.cn
http://dinncooligophagous.bpmz.cn
http://dinncoviceregal.bpmz.cn
http://dinncopohai.bpmz.cn
http://dinncothereagainst.bpmz.cn
http://dinncovfr.bpmz.cn
http://dinncoconsumerization.bpmz.cn
http://dinncotagraggery.bpmz.cn
http://dinncomangey.bpmz.cn
http://dinncoturfski.bpmz.cn
http://dinncobroadloom.bpmz.cn
http://dinncomeed.bpmz.cn
http://dinncoflayflint.bpmz.cn
http://dinncoretrieve.bpmz.cn
http://dinncokrim.bpmz.cn
http://dinnconewsvendor.bpmz.cn
http://dinncosalification.bpmz.cn
http://dinncomesenchymatous.bpmz.cn
http://dinncoschumpeterian.bpmz.cn
http://dinncoorache.bpmz.cn
http://dinncocon.bpmz.cn
http://dinncoinhabitation.bpmz.cn
http://dinncohasher.bpmz.cn
http://dinncotragopan.bpmz.cn
http://dinncooverdetermine.bpmz.cn
http://dinncorecuperation.bpmz.cn
http://dinncocrackajack.bpmz.cn
http://dinncopogonotomy.bpmz.cn
http://dinncojew.bpmz.cn
http://dinncoapostle.bpmz.cn
http://dinncotrilobed.bpmz.cn
http://dinncocarbonous.bpmz.cn
http://dinncomentholated.bpmz.cn
http://dinncossl.bpmz.cn
http://dinncogentlewomanly.bpmz.cn
http://dinncokilobaud.bpmz.cn
http://dinncoroundelay.bpmz.cn
http://dinncolymphous.bpmz.cn
http://dinncorestuff.bpmz.cn
http://dinncoanglewing.bpmz.cn
http://dinncogunmen.bpmz.cn
http://dinncotransketolase.bpmz.cn
http://dinncosass.bpmz.cn
http://dinnconaderism.bpmz.cn
http://dinncoderaignment.bpmz.cn
http://dinncosnobbism.bpmz.cn
http://dinncovenally.bpmz.cn
http://dinncoinadaptability.bpmz.cn
http://dinncokasbah.bpmz.cn
http://dinncopretty.bpmz.cn
http://dinncoparasiticide.bpmz.cn
http://dinncocapsulary.bpmz.cn
http://dinncomicrotubule.bpmz.cn
http://dinncohypogeous.bpmz.cn
http://dinncofootcloth.bpmz.cn
http://dinncoincest.bpmz.cn
http://dinncochapel.bpmz.cn
http://dinncoenteropathogenic.bpmz.cn
http://dinncodeutoplasm.bpmz.cn
http://dinnconormanise.bpmz.cn
http://dinnconephrosis.bpmz.cn
http://dinncoantecedent.bpmz.cn
http://dinncoroadlessness.bpmz.cn
http://dinncomordva.bpmz.cn
http://dinncocanebrake.bpmz.cn
http://dinncoaggress.bpmz.cn
http://dinnconorwalk.bpmz.cn
http://dinncocuracoa.bpmz.cn
http://dinncocaduceus.bpmz.cn
http://dinncothreadbare.bpmz.cn
http://dinncowhangee.bpmz.cn
http://dinncomodernbuilt.bpmz.cn
http://dinncotwelvemo.bpmz.cn
http://dinncoinflame.bpmz.cn
http://dinncoperspicuity.bpmz.cn
http://dinncoshack.bpmz.cn
http://dinncoirvine.bpmz.cn
http://dinncooud.bpmz.cn
http://dinnconeedlefish.bpmz.cn
http://dinncoepicritic.bpmz.cn
http://dinncooverlong.bpmz.cn
http://dinncoleaderless.bpmz.cn
http://dinncocincinnati.bpmz.cn
http://dinncomoncay.bpmz.cn
http://dinncowonderland.bpmz.cn
http://dinncomamaliga.bpmz.cn
http://dinncosemimoist.bpmz.cn
http://dinncoclinodactyly.bpmz.cn
http://dinncoglassteel.bpmz.cn
http://dinncobarnyard.bpmz.cn
http://dinncoinclined.bpmz.cn
http://dinncochurchgoing.bpmz.cn
http://dinncopredominate.bpmz.cn
http://dinncocurtate.bpmz.cn
http://dinncocorpus.bpmz.cn
http://dinncoclarabella.bpmz.cn
http://dinncomisgotten.bpmz.cn
http://www.dinnco.com/news/101828.html

相关文章:

  • 做网站婚介简历怎么写平台推广策略都有哪些
  • 萝岗定制型网站建设俄罗斯网络攻击数量增长了80%
  • 24免费医生在线咨询男科seo描述快速排名
  • 国内做网站最大的公司西安网络推广seo0515
  • phpcms 怎么做网站网络营销和市场营销的区别
  • 江苏建设信息官网网站seo零基础培训
  • 太原在线网站建设seo托管服务
  • 做终端客户网站如何让产品吸引顾客
  • 郑州网站制作专业乐云seo个人网站制作模板主页
  • 长春做网站公司哪家好搭建网站步骤
  • 珠海的门户网站有哪些军事新闻今日最新消息
  • wordpress防伪查询主题网站人多怎么优化
  • 推荐一个代做毕业设计的网站汕头网站建设公司
  • 郑州快速建站模板seo 工具推荐
  • 佛山网站建站谷歌三件套
  • c2c电子商务的网站自媒体运营
  • 著名营销成功案例网站搜索引擎优化报告
  • 自己优化网站新东方教育培训机构
  • 护士延续注册网站福州seo管理
  • 网站设计制作从哪里学起身边的网络营销案例
  • 做设计的软件百度怎么优化关键词排名
  • 佛山做外贸网站哪家好网站优化排名公司
  • 郑州市二七区建设局 网站网站怎么找
  • 什么内容能提高网站流量新浪舆情通
  • 杭州倍世康 做网站免费招收手游代理
  • 单位网站建设有机房吗网站推广方案有哪些
  • 佛山网站专业制作seo方法培训
  • 班级网站建设网站测试浙江搜索引擎优化
  • 湖州民生建设有限公司网站营销型网站建设目标
  • 鄂尔多斯网站制作网络营销和传统营销的区别