c - 如何修复 "error: expected ; , or ) before"字符串参数

标签 c shell compiler-errors malloc expected-condition

我们正在尝试为我们的项目使用 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)

最佳答案

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

关于c - 如何修复 "error: expected ; , or ) before"字符串参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58032053/

相关文章:

c - 禁止 C 预处理器更改缩进

将 char 数组复制到数组的元素

linux - 遍历文件夹和子文件夹中的文件名

使用 `seq` 的 Bash 通配符模式

c++ - 错误 C++ 2679(二进制 '>>' : no operator found which takes a right-hand operand of type 'const std::string' (or there is no acceptable conversion))

excel - 为什么宏给出编译错误而脚本中没有任何逻辑或语法错误?

c - 在 C 中从函数返回 scanf 值

c - 带有字符串变量的 fopen 失败

linux - sed 查找并替换 IP 地址后包含字符串的行

java - 错误 : The method add(java. lang.String) 未定义类型 java.lang.Object