【问题标题】:Newbie: C syntax error when compiling新手:编译时出现C语法错误
【发布时间】:2012-07-03 05:28:52
【问题描述】:
    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;
            }
    }

当我尝试编译时,第一行 [for(int...] 出现此错误:

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

为什么我需要添加另一个')'?

【问题讨论】:

  • 在我看来像冒泡排序:P
  • 当您在 ';' 之前看到类似“error: expected ')' 的错误时令牌”有两种查看方式。您可能需要一个额外的')',或者像这里的情况一样,您需要删除一个';'。一个有用的技巧是同时考虑这两种可能性。

标签: c for-loop comma-operator


【解决方案1】:
for(int a = 0, b = 1; b < n; a++; b++)
                                ^
                                |
                              problem

您需要在 for 循环的末尾使用逗号 (,) 而不是分号 (;) 来递增 ab

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

这是comma operator

这两个 SO 问题也可能会有所帮助:How do I put two increment statements in a C++ 'for' loop?What is the full "for" loop syntax in C (and others in case they are compatible)?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-06-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-10
    • 1970-01-01
    相关资源
    最近更新 更多