繁体   English   中英

C代码中的语法错误

[英]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*/
    }
}
}

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

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

有什么建议为什么会这样吗?

如果size是定义为常量的宏,例如`

#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;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM