gpt4 book ai didi

c - 我想从缓冲区中的元素复制指定长度的字符串

转载 作者:行者123 更新时间:2023-11-30 20:11:50 26 4
gpt4 key购买 nike

我有数据缓冲区。从特定字符(即 A 字符)开始,我想将接下来的 40 个元素复制到另一个缓冲区中,并想丢弃其余的元素。我作为函数参数的缓冲区

char *MyBuff(unsigned char *input)

为了在该缓冲区中搜索元素,我使用 for 循环。

for (i = 0; input[i] != NULL; i++) {
if (input[i] == 'MyElement') {
// from that element I wanted to copy data till 40th element
for (i = 1; i <= 41; i++) {
output[i] = input[i];
input++;
}
}
}
return output;

但是从上面我无法接收任何数据。我缺少什么。?在这里粘贴完整功能..

unsigned char output[42];
char *MyBuff(unsigned char *input) {
char i;
for (i = 0; input[i] != NULL; i++) {
// search from starting of input array
if (input[i] = 'a') { //if character is found
for (i = 1; i <= 41; i++) {
// copy next 41 character in ouput
ouput[i] = input[i];
input++;
}
}
}
return output; // return the buffer with output
}

最佳答案

我没有看到您的完整函数,但假设您为输出分配了足够的空间,并使用malloc分配了它。您基本上对内部循环和外部循环使用相同的索引变量i。您应该将内部的值更改为 j 或其他内容,并以 j = 0 开始循环。所以你的固定代码应该是:

int i, j;
for(i=0; input[i] != NULL; ++i)
{
if(input[i] == 'X') // looking for uppercase X character
{
//from that element I wanted to copy data till 40th element
for (j = 0; j < 41 || input[i+j] == 0; j++)
{
output[j] = input[i+j];
}
break; // so we don't search again
}
}
output[41] = 0; // need null byte for end of string
return output;

关于c - 我想从缓冲区中的元素复制指定长度的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39011440/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com