12

I am writing some code where I need to use two variables in a for loop. Does the below code seem alright?

It does give 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 am getting a compiler warning which says:

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

What does this mean?

3
  • 6
    wow, I am staring at this code and no matter how hard I try I can barely understand what it is doing :-) I have always admired C programmers. Commented Oct 16, 2011 at 9:00
  • 2
    @DarinDimitrov If you want I can rewrite it in C#, but it will be the same :-) Commented Oct 16, 2011 at 9:22
  • @xanatos, oh I sincerely hope you would not write anything like this in C#. I mean pointers? Come on, the BCL certainly has something built-in to achieve this (whatever this code is trying to achieve). Commented Oct 16, 2011 at 9:23

3 Answers 3

33

The problem is the test condition:

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

This does not check both parts. (Well, it does, 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.

Sign up to request clarification or add additional context in comments.

Comments

20

Mat is correct, but you should probably consider simplifying your code to:

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++;
    }
}

(You can obviously hoist the calculation of in_payload + offset out of the loop too, but the optimiser will almost certainly do that for you).

Comments

1

For your semantically problems see caf's answer. First try to straight out your thoughts before starting to type.

One misunderstanding is that you are mixing up two different concepts of C, initialization and assignment. Obviously in your code you are thinking in the lines of an initialization where the thing with the comma would work perfectly. So the next time you encounter a similar problem, just use local variables. These are valid constructs in C99, and a good thing to use, anyhow.

You didn't give us the type of the variables but assuming size_t your for statement would look like

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.