Two variables in a 'for' loop in C

I am writing code where I need to use two variables in a loop for. Does the code below look good?

This gives me the expected result.

for (loop_1 = offset,loop_2 = (offset + 2); loop_1 >= (offset - 190),loop_2 <= (190 + offset + 2); loop_1--,loop_2++)
{
    if (  (*(uint8_t*)(in_payload + loop_1) == get_a1_byte(bitslip)) &&
         ((*(uint8_t*)(in_payload + loop_2) == get_a2_byte(bitslip)))
       )
    {
          a1_count++;
    }
}

But I get a compiler warning saying:

file.c: 499: 73: warning: left operand of a comma expression has no effect

What does it mean?

+5
source share
3 answers

The problem is the check condition:

loop_1 >= (offset - 190),loop_2 <= (190 + offset + 2)

This does not check both parts. (Well, it is, but only the result of the second part is used.)

Change it to

(loop_1 >= (offset - 190)) && (loop_2 <= (190 + offset + 2))

if you want both conditions to be checked.

+24
source

Mother is correct, but you should probably consider simplifying the code:

for (i = 0; i <= 190; i++)
{
    uint8_t *pl1 = (uint8_t *)(in_payload + offset - i);
    uint8_t *pl2 = (uint8_t *)(in_payload + offset + i + 2);

    if (*pl1 == get_a1_byte(bitslip) && *pl2 == get_a2_byte(bitslip))
    {
        a1_count++;
    }
}

(, in_payload + offset , ).

+16

. . , .

, C, . , , . , , . C99 . .

, size_t for

for (size_t loop_1 = offset, loop_2 = (offset + 2);
     loop_1 >= (offset - 190) && loop_2 <= (190 + offset + 2);
     loop_1--, loop_2++)
+1

All Articles