1
    for(int a = 0, b = 1; b < n; a++; b++)
    {
        if (compare(values[a], values[b]))
            counter++;
        else
            {
            int x = values[a];
            values[a] = values[b];
            values[b] = x;
            }
    }

I get this error for the first line [ for(int... ] when I try to compile:

helpers.c:68:41: error: expected ')' before ';' token

Why would I need to add another ')'?

Daniel Daranas
  • 22,454
  • 9
  • 63
  • 116
hannah
  • 889
  • 4
  • 13
  • 27
  • looks like a bubble sort to me :P – Wug Jul 02 '12 at 19:16
  • When you see an error like "error: expected ')' before ';' token" there are two ways to look at it. Either you need an extra ')' as you suspected, or as is the case here, you need to remove a ';'. A useful trick is to consider both possibilities. – David Heffernan Jul 02 '12 at 21:00

1 Answers1

11
for(int a = 0, b = 1; b < n; a++; b++)
                                ^
                                |
                              problem

You need a comma (,) rather than a semicolon (;) at the end of your for-loop where you increment both a and b:

for(int a = 0, b = 1; b < n; a++, b++)
                                ^

This is the comma operator.

These two SO questions might also be helpful: How do I put two increment statements in a C++ 'for' loop? and What is the full "for" loop syntax in C (and others in case they are compatible)?

Community
  • 1
  • 1
Levon
  • 138,105
  • 33
  • 200
  • 191