Search code examples
cbubble-sort

Syntax Error in C code


void bubble (char cList[] ,int size)  {  // This is the line with the error
int swapped;
int p;
for (p = 1; p < size ; p++)
{
    swapped = 0;    /*this is to check if the array is already sorted*/
    int j;
    for(j = 0; j < size - p; j++)
    {
        if(cList[j] > cList[j+1])
        {
            int temp = cList[j];
            cList[j] = cList[j+1];
            cList[j+1] = temp;
            swapped = 1;
        }
    }
    if(!swapped)
        {
        break; /*if it is sorted then stop*/
    }
}
}

This is a snippet of my code. size is a constant that I declared already. cList is an array of clients. I keep getting the error:

expected ';', ',' or ')' before numeric constant

Any suggestions why this is happening?


Solution

  • If size is a macro defined as a constant, e.g. `

    #define size 1234
    

    then the code will read as, for example,

    void bubble (char cList[] ,int 1234)  {  
    

    for which that is the correct error message.

    To correct this, just remove the argument. Wherever size is seen the number will be substituted as a text change to the program before compilation. E.g.:

    for (p = 1; p < size ; p++)
    

    becomes

    for (p = 1; p < 1234 ; p++)
    

    BONUS

    ALWAYS define numerical constants in brackets! It should read

    #define size (1234)
    

    See: http://en.wikibooks.org/wiki/C_Programming/Preprocessor#.23define

    BONUS BONUS

    Unless you have a good reason, it's best to use real constant variables, as Alexey Frunze says in the comment. Consider:

    const int size = 1234;