【问题标题】:error: expected '=', ',', ';', 'asm' or '__attribute__' before '{' token错误:在 '{' 标记之前应有 '='、','、';'、'asm' 或 '__attribute__'
【发布时间】:2015-04-18 15:30:08
【问题描述】:

这是主要的。

int
main(int argc, char *argv[]) {
    char statement[MAX_LINE];
    int statement_len;
    char type[MAX_LINE];
    char var[MAX_LINE];

    /* Print the output header comment */
    printf(OUTPUT_HEADER, argv[0]);

    /* Loop through statements read on stdin */
    while ((statement_len = next_statement(statement,MAX_LINE)) > 0) {
        printf("%s;\n",statement);
        sscanf(statement,"%s %s",type,var);
        var_lib_check(type,var);
        var_replace(statement,statement_len);
    }
    return 0;
}

这是发生错误的函数。

void
var_replace(char statement, int statement_len){
int i;
int x;

for (i = 0; i < statement_len; i++){
    for (x = 0; x < num_of_var; x++){
        if (strcmp(var_library[x],statement[i]) == 0){
            printf("hello");
        }
    }
}
return;
}

错误:

在“{”标记之前应为“=”、“,”、“;”、“asm”或“属性

我该如何解决这个问题?

【问题讨论】:

  • 编译器指向哪一行?我们不是 GCC 或 clang,你知道的
  • 错误可能在那个函数之前,你可能没有匹配的大括号。
  • 语句是一个 char 变量,而您在 strcmp() 行中将它视为一个 char 数组。
  • 您现在应该担心其他事情,例如传递char 并在其上使用索引。 c中没有字符串类型,char != string.
  • 我修复了它,因为我忘记了#include 并且我更改了数组指针。

标签: c compiler-errors


【解决方案1】:

你的功能是

void var_replace(char statement, int statement_len){

因此,它需要一个字符作为第一个参数。但是你传递的是一个字符串

char statement[MAX_LINE];
var_replace(statement,statement_len);
               ^
              statement is a character array

这是一个问题。然后在你的函数中,你使用

if (strcmp(var_library[x],statement[i]) == 0){
                                    ^
                                  but statement is a character, not a string

如果要检查每个字符,请不要使用strcmp()

【讨论】:

  • 这如何回答这个问题?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多