给定一个长度为 n 的 char 数组缓冲区,从位置 pos 删除 m 个字符

发布于 2025-01-16 19:48:20 字数 347 浏览 0 评论 0原文

我想从无符号字符缓冲区中删除字符

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

黑寡妇 2025-01-23 19:48:20

这段代码根本没有优化,但显示了我认为最容易理解的方法:


#include <stdlib.h>
#include <stdio.h>
#include <string.h>
void delete(int p, const char * str){
    char *done;
    int t = strlen(str); 
    if(p>t||p<=0){
        return; 
    }else{
        strcpy(done, str);
        strcpy(done+p-1,str+p); 
    }
    strcpy(str, done);
}
int main(int argc, char **argv){
    char buff[] = "abcdefghi"; 
    delete(3, buff); 
    printf(buff);//it prints abcdefghi
    return 0;
}

这个想法很简单 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:


#include <stdlib.h>
#include <stdio.h>
#include <string.h>
void delete(int p, const char * str){
    char *done;
    int t = strlen(str); 
    if(p>t||p<=0){
        return; 
    }else{
        strcpy(done, str);
        strcpy(done+p-1,str+p); 
    }
    strcpy(str, done);
}
int main(int argc, char **argv){
    char buff[] = "abcdefghi"; 
    delete(3, buff); 
    printf(buff);//it prints abcdefghi
    return 0;
}

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) to done+p-1 as we want to erase/overlap the char at the position p

Finally we just copy done, to the original string(str) and we have successfully erased a character


Also 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文