-3

I have buffer with my data. From a particular character i.e A character I wanted to copy next 40 elements into another buffer and I wanted to discard the rest. Buffer I took as function argument

char *MyBuff(unsigned char *input)

for searching element in that buffer I am using for loop.

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;

But from above I am not able to receive any data. What I am missing.? Pasting full function here..

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
}
Ax Cool
  • 115
  • 1
  • 6
  • Can you show some actual working code that exhibits the issue? At present we cant be sure what you are expecting. An [m.c.v.e](http://stackoverflow.com/help/mcve) would be helpful. At the very least the full definition of the function from which your code is taken. – Paul Rooney Aug 18 '16 at 06:41
  • 1
    Is `output[]` a local array? – Haris Aug 18 '16 at 06:41
  • 1
    What does input++ do here? input is an array, you are already iterating over all elements using the for loop. I'd suggest removing that input++ from the code. – xCodeZone Aug 18 '16 at 06:43
  • ok I am pasting my function after editing. . TY for Reply – Ax Cool Aug 18 '16 at 06:43
  • `if(input[i] == 'MyElement'` `input[i]` is a single character It can at most be `M`. If you want to check that the string "MyElement" exists at position `i` you need a loop to do that. – Rishikesh Raje Aug 18 '16 at 06:45
  • What's `if(input[i] == 'MyElement')` supposed to be? That's nonsense. Read a C book. – Lundin Aug 18 '16 at 06:45
  • Hi @Haris , output[] is extern array, and, xCodeZone :- I surely remove input++. – Ax Cool Aug 18 '16 at 06:45
  • 'if(input[i] == 'MyElement')' What are you trying to do here? You are comparing a character with a string? – Karthick Aug 18 '16 at 06:45
  • @Lundin :- I may be novice or new here., But I am searching for An Element ,Say its 'a' , If I got that element, I wanted to copy 40 members after that. I am wrong thats why output is not coming., But i need advice – Ax Cool Aug 18 '16 at 06:47
  • @Karthick I wanted to compare string element with character 'a', I need to edit `if(input[i] = 'a'). @RishikeshRaje : I agree with your point , Loop I have puted `for` , Its not working. – Ax Cool Aug 18 '16 at 06:49
  • @AxCool You need to study the basics of strings. Here's a start: http://stackoverflow.com/questions/8004237/how-do-i-properly-compare-strings-in-c – Lundin Aug 18 '16 at 06:52
  • @Lundin:- thanks for article., Yes admitted , I need to explore lot.. – Ax Cool Aug 18 '16 at 06:54
  • Hi all , thanks all for inputs, I am able to solve the mistake. – Ax Cool Aug 18 '16 at 07:32
  • Your function doesn't check if the buffer has those 40 characters. What's happen if your buffer doesn't have them ? – Michi Aug 18 '16 at 12:44
  • @Michi: point made, So for that I need to run loop for checking if chars are there? If buffer does not have them, I have to fill it. – Ax Cool Aug 19 '16 at 07:48
  • @AxCool Consider [The following](http://ideone.com/VfVYCh) an Example of what I mean. – Michi Aug 19 '16 at 15:33

1 Answers1

2

I don't see your complete function but assume you allocated enough space for output and allocated it with malloc. You are basically using the same index variable i for both inner and outer loops. You should change the inner one to j or something and start the loop with j = 0. So your fixed code should be:

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;
Fma
  • 362
  • 1
  • 11