给定一个长度为 n 的 char 数组缓冲区,从位置 pos 删除 m 个字符
我想从无符号字符缓冲区中删除字符
C 语言:
unsigned char buff[n] = "...."; // len n byte array
我想到使用 memmove
memmove(buff + pos, buff + pos + m, ???);
任何人都可以帮忙解决 ??? 的值应该是多少吗?是的,我得到了删除 m 个字符的正确结果,无论 ??? value (n - pos 到 n 之间的任意值)
m 和 pos 的值小于 n 并且 buff 是字节数组(数据包)
I want to delete characters from a unsigned char buffer
C language:
unsigned char buff[n] = "...."; // len n byte array
I thought of using memmove
memmove(buff + pos, buff + pos + m, ???);
Can anyone help with what should the value of ??? be, I am getting the correct result of m characters deleted irrespective of ??? value (any value between n - pos to n)
Values of m and pos are less than n and the buff is a byte array (A data packet)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这段代码根本没有优化,但显示了我认为最容易理解的方法:
这个想法很简单 strcpy 接受两个参数,第一个是指向目标字符串的指针(
str
),第二个是指向原点的指针。我们首先将整个原始字符串(
str
)复制到临时字符串(done
)然后我们只需复制
str+p
(p是字符串上从 1 开始到 strlen 结束的位置,因为 strlen 排除了我们检查到最后一个位置的终止符字节)到done+p-1
因为我们想要擦除/重叠该位置处的字符p最后我们复制
done
,到原始字符串(str
),我们已经成功删除了一个字符此外,由于我们使用的是strcpy,该函数会处理终止符字节,我们也不用'尽管编译器会抱怨原始字符串的转换,但不必非常注意释放临时指针,这很容易解决,但可以很容易地找出我在答案中保留的逻辑
well this code is not optimized at all but shows what i think is the easiest to understand method:
The idea is simple strcpy takes two parameters the first being a pointer to the destination string(
str
) and the second a pointer to origin.we start by copying the whole original string(
str
) to our temporary string(done
)Then we just copy
str+p
( p being the position on the string starting at 1 and ending at strlen, since strlen excludes the terminator byte we check till the last position) todone+p-1
as we want to erase/overlap the char at the position pFinally we just copy
done
, to the original string(str
) and we have successfully erased a characterAlso since we are using strcpy, the function takes care of the terminator byte, we also don't have to pay much attention to freeing the temporary pointer although the compiler will complain about the casting of the origin string's which are fairly simple to resolve but to keep it easy to figure out the logic I've kept out of the answer