简体   繁体   English

在范围内使用memcpy或memcmp

[英]Using memcpy or memcmp with ranges

Is it possible to use a range when using memcpy or memcmp? 使用memcpy或memcmp时可以使用范围吗?

char data[900000]; // size 900000
char array[20]; // size 20

if (memcmp(data[50-70], array, 20) == 0) {
    // do thing
}

I'd like to be able to compare the (20) keys data[50-70] with array[] 我希望能够将(20)个键数据[50-70]与array []进行比较

memcmp / memcpy simply take a pointer to the data you want to compare or copy. memcmp / memcpy只需获取要比较或复制的数据的指针。

Thus, you can essentially copy or compare any "range" by providing a pointer to the start of the data you wish to compare and the length of the data, pretty much as you have done above. 因此,您基本上可以通过提供指向您要比较的数据的开头和数据长度的指针来复制或比较任何“范围”,就像您在上面所做的一样。

Adjust your code above as follows: 调整上面的代码,如下所示:

if (memcmp(&data[50], array, 20) == 0) {
    // do thing
}

This tells memcmp to start checking at the address of the 50th subscript of the data array, and to compare that to the data at the address of array, and to check 20 elements. 这告诉memcmp从数据数组第50个下标的地址开始检查,并将其与数组地址的数据进行比较,并检查20个元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM