【问题标题】:How to fix "error: expected ; , or ) before" string parameter如何修复“错误:预期;,或)之前”字符串参数
【发布时间】:2020-01-21 17:31:18
【问题描述】:

我们正在尝试在 c 中为我们的项目实现一个 bash shell。现在我正在创建一个函数 addAlias,它基于我们的 AddToken 函数(填充我们的 strArray 结构)。

strArray 结构有一个 **char(字符串数组)和 int,表示数组中的标记数。 同样, aliasArray 结构有两个 **char 和一个 int 用于别名数量。实现几乎相同,所以我不明白 addAlias 函数中的错误来自哪里。

我已经尝试搜索 Stack,并移动代码,但我不知道是什么原因造成的。

这是来自头文件:

typedef struct
{
    char** shortcuts;
    char** notshort;
    int numaliases;
} aliasArray;

void addAlias(aliasArray* instr_ptr, char* short, char* long);

这是来自 c 文件的函数,它应该分别使用参数给出的快捷方式及其别名填充两个数组:

void addAlias(aliasArray* instr_ptr, char* short, char* long)
{
    //extend token array to accomodate an additional token
if (instr_ptr->numaliases == 0)
{
    instr_ptr->shortcuts = (char**) malloc(sizeof(char*));
    instr_ptr->notshort = (char**) malloc(sizeof(char*));
}
else
{
    instr_ptr->shortcuts = (char**) realloc(instr_ptr->shortcuts, (instr_ptr->numaliases+1) * sizeof(char*));
    instr_ptr->notshort = (char**) realloc(instr_ptr->notshort, (instr_ptr->numaliases+1) * sizeof(char*));
}

    //allocate char array for new token in new slot
instr_ptr->shortcuts[instr_ptr->numaliases] = (char *)malloc((strlen(short)+1) * sizeof(char));
instr_ptr->notshort[instr_ptr->numaliases] = (char *)malloc((strlen(long)+1) * sizeof(char));
strcpy(instr_ptr->shortcuts[instr_ptr->numaliases], short);
strcpy(instr_ptr->shortcuts[instr_ptr->numaliases], long);

instr_ptr->numaliases++;

}

这些是我们主函数的声明: aliasArray 别名;

aliases.shortcuts = NULL;
aliases.notshort = NULL;
aliases.numaliases = 0;

这是我为此部分添加的所有代码,当我尝试将其注释掉并使用 gcc -g 运行所有内容时,我收到了一些不相关的警告,但一切运行正常。 当它没有被注释掉并且我尝试运行它时,我得到了这些错误:

In file included from commandler.c:1:0:
commandler.h:17:44: error: expected â;â, â,â or â)â before âshortâ
 void addAlias(aliasArray* instr_ptr, char* short, char* long);
                                        ^
commandler.c:295:44: error: expected â;â, â,â or â)â before âshortâ
 void addAlias(aliasArray* instr_ptr, char* short, char* long)

【问题讨论】:

  • shortlongC 中的保留字(如 intvoid)。您不能将它们用作变量名。
  • shortlong 是关键字,不能用作标识符。尝试使用一些好的编辑器或 IDE,它们可以帮助您通过颜色识别关键字/标识

标签: c shell compiler-errors malloc expected-condition


【解决方案1】:

根据 C 标准(6.4.1 关键字)

2 上述标记(区分大小写)是保留的(翻译中) 阶段 7 和 8) 用作关键字,nd 不得用于其他用途

和(6.4.2 标识符)

4 在翻译过程中将预处理标记转换为标记时 阶段 7,如果预处理令牌可以转换为 关键字或标识符,转换为关键字

在函数原型中你可以省略参数标识符

void addAlias( aliasArray*, char*, char* );

在函数定义中,参数名称必须使用其他标识符而不是关键字。

例如,您可以为参数使用与结构中的名称相同的名称

void addAlias(aliasArray* instr_ptr, char* shortcut, char* nonshortcut)

还要考虑到参数应该有限定符const

void addAlias(aliasArray* instr_ptr, const char* shortcut, const char* nonshortcut)

否则函数声明会迷惑函数的使用者,虽然函数没有改变对应的字符串,但不能接受常量指针。

【讨论】:

    【解决方案2】:

    shortlong 是 C 中的保留字(如 intvoid)。

    您不能将它们用作变量名。

    建议更改:

    void addAlias(aliasArray* instr_ptr, char* shortName, char* longName)
    

    现在变量名是shortNamelongName,不再是保留字了。

    【讨论】:

      【解决方案3】:

      char* shortchar* long 是问题所在。 shortlong 是 C 中的类型名称。您应该使用另一个参数名称。

      【讨论】:

      • 包含一些链接以添加另一层解释总是一个好主意。喜欢指向 short 的链接和进一步阅读 what can't be an identifier 的链接或将它们包含在答案本身中。
      猜你喜欢
      • 2020-12-05
      • 1970-01-01
      • 2013-12-16
      • 2020-11-16
      • 1970-01-01
      • 2023-03-07
      • 1970-01-01
      • 2017-09-22
      相关资源
      最近更新 更多