C中的预期标识符错误

C中的预期标识符错误,c,identifier,C,Identifier,出于某种原因,我得到了错误: expected identifier or '(' before 'wordlist' 在我的头文件中(以及相应的函数定义),用于返回wordlist指针的两个函数 使用以下代码: #ifndef FUNCTIONS_H #define FUNCTIONS_H typedef struct word{ char *string; struct word* next; }word; typedef struct wordlist{ word

出于某种原因,我得到了错误:

expected identifier or '(' before 'wordlist'
在我的头文件中(以及相应的函数定义),用于返回
wordlist
指针的两个函数

使用以下代码:

#ifndef FUNCTIONS_H
#define FUNCTIONS_H

typedef struct word{
   char *string;
   struct word* next;
}word;

typedef struct wordlist{
   word *head;
   word *tail;
}wordlist;

*wordlist populateList(FILE *file);

*wordlist encrypt(wordlist *wl, int rotation);

void toFile(wordlist *wl, char *outputFileName);

#endif

有人能告诉我为什么会这样吗?

如果在
populateList
encrypt
中,您想返回一个指向
wordlist
的指针,正确的语法是
wordlist*
,而不是
*wordlist
(与其他地方完全一样).

如果在
populateList
encrypt
中要返回指向
wordlist
的指针,正确的语法是
wordlist*
,而不是
*wordlist
(与其他地方完全相同)。

这是因为在声明指针时,星号必须紧跟类型名称,而不是在类型名称之前:

wordlist * populateList(FILE *file);
//       ^
//       |
//      Here

这是因为在声明指针时,星号必须位于类型名称之后,而不是之前:

wordlist * populateList(FILE *file);
//       ^
//       |
//      Here

如果要定义或声明变量,请指定变量类型,后跟变量名称。因此,如果您想要
wordlist
类型的变量,您可以使用:

wordlist myVariable;
wordlist *myVariable;
如果要将变量指定为指向变量类型的指针,请在变量名称前加星号,因此如果要将变量指定为指向
wordlist
类型变量的指针,请使用:

wordlist myVariable;
wordlist *myVariable;
大多数有经验的C程序员之所以在变量名中加上星号,是因为如下原因:

wordlist myVariable, *pVariable1, myVariable2, *pVariable2;
上面将创建四个变量
myVariable
的类型为
wordlist
myVariable2
属于
wordlist
类型
pVariable1
pVariable2
属于指向
wordlist
的指针类型

因此,星号充当变量名称声明的一种形容词、限定词或修饰符,表示变量不是指定的类型,而是指向指定类型的指针

组合变量定义与以下四行定义相同

wordlist myVariable;  // declares a variable of type wordlist
wordlist *pVariable1; // declares a pointer to a variable of type wordlist
wordlist myVariable2; // declares a variable of type wordlist
wordlist *pVariable2;  // declares a pointer to a variable of type wordlist
函数定义/声明的工作方式类似

wordlist *myFunc (void) {
    wordlist *myNew = malloc (sizeof(wordlist));

    if (myNew) {
        // set up myNew stuff
    }

    return myNew;
}
编辑:函数指针

还可以指定包含函数指针的变量。例如,对于上面的myFunc(),可以指定如下内容。请注意,我使用括号强制执行特定的求值顺序。这说明pFunc是指向不接受参数(无效参数列表)的函数的指针,它返回指向wordlist变量的指针。C语言中有关于运算符和修饰符优先级的规则,但是随着表达式变得越来越复杂,通常最好使用括号强制执行求值顺序。看


如果要定义或声明变量,请指定变量类型,后跟变量名称。因此,如果您想要
wordlist
类型的变量,您可以使用:

wordlist myVariable;
wordlist *myVariable;
如果要将变量指定为指向变量类型的指针,请在变量名称前加星号,因此如果要将变量指定为指向
wordlist
类型变量的指针,请使用:

wordlist myVariable;
wordlist *myVariable;
大多数有经验的C程序员之所以在变量名中加上星号,是因为如下原因:

wordlist myVariable, *pVariable1, myVariable2, *pVariable2;
上面将创建四个变量
myVariable
的类型为
wordlist
myVariable2
属于
wordlist
类型
pVariable1
pVariable2
属于指向
wordlist
的指针类型

因此,星号充当变量名称声明的一种形容词、限定词或修饰符,表示变量不是指定的类型,而是指向指定类型的指针

组合变量定义与以下四行定义相同

wordlist myVariable;  // declares a variable of type wordlist
wordlist *pVariable1; // declares a pointer to a variable of type wordlist
wordlist myVariable2; // declares a variable of type wordlist
wordlist *pVariable2;  // declares a pointer to a variable of type wordlist
函数定义/声明的工作方式类似

wordlist *myFunc (void) {
    wordlist *myNew = malloc (sizeof(wordlist));

    if (myNew) {
        // set up myNew stuff
    }

    return myNew;
}
编辑:函数指针

还可以指定包含函数指针的变量。例如,对于上面的myFunc(),可以指定如下内容。请注意,我使用括号强制执行特定的求值顺序。这说明pFunc是指向不接受参数(无效参数列表)的函数的指针,它返回指向wordlist变量的指针。C语言中有关于运算符和修饰符优先级的规则,但是随着表达式变得越来越复杂,通常最好使用括号强制执行求值顺序。看


C的标准格式是错误的。通常您会看到
someType*someVarsomeVar
的类型是
someType*
这一点不那么明显。我更喜欢写
someType*someVar
使关联更清晰,顺便说一句,使人们不太可能错误地为方法声明中的返回类型编码
*someType
。@HotLicks-这真的有点费劲。编写
someType*someVar,someVar2
可以很容易地假设
someVar2
属于
someType*
类型,而实际上它只是
someType
。出于这个原因,我发现
someType*someVar,*someVar2
更好。另一条规则是永远不要使用
typevar1,var2…
。这里有太多的陷阱,而且在功能上是不必要的。C的标准格式是骗人的。通常您会看到
someType*someVarsomeVar
的类型是
someType*
这一点不那么明显。我更喜欢写
someType*someVar
使关联更清晰,顺便说一句,使人们不太可能错误地为方法声明中的返回类型编码
*someType
。@HotLicks-这真的有点费劲。编写
someType*someVar,someVar2
很容易假设<