1
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*/
    }
}
}

这是我的代码片段。size是我已经声明的常数。cList是一组客户。我不断收到错误:

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

有什么建议为什么会发生这种情况?

4

1 回答 1

5

Ifsize是一个定义为常量的宏,例如 `

#define size 1234

那么代码将读取为,例如,

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

这是正确的错误消息。

要纠正这个问题,只需删除参数。无论在哪里size看到该数字都将在编译之前替换为程序的文本更改。例如:

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

变成

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

奖金

总是在括号中定义数字常量!它应该读

#define size (1234)

请参阅:http ://en.wikibooks.org/wiki/C_Programming/Preprocessor#.23define

奖金奖金

除非您有充分的理由,否则最好使用实常数变量,正如 Alexey Frunze 在评论中所说。考虑:

const int size = 1234;
于 2013-04-09T15:21:13.553 回答