我的数据缓冲区。从一个特定的字符,即一个字符,我想将下一个40个元素复制到另一个缓冲区,我想丢弃其余的。 我把Buffer作为函数参数
char *MyBuff(unsigned char *input)
用于搜索我用于循环的缓冲区中的元素。
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
}
答案 0 :(得分:2)
我没有看到您的完整功能,但假设您为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;