链接列表和指针语法错误

kat*_*iea 2 c syntax struct pointers

此程序只需使用ASCII行文件,将其放入链接列表堆栈,然后将反转列表打印为相同ASCII格式的新文件.

我的结构代码:

typedef struct Node{
    char info[15];
    struct Node *ptr;
};
Run Code Online (Sandbox Code Playgroud)

我在Main上遇到以下错误.大多数人都必须在我声明新节点头的地方做...那个语法出了什么问题?:

Errors
    strrev.c:28: error: ‘Node’ undeclared (first use in this function)
    strrev.c:28: error: (Each undeclared identifier is reported only once
    strrev.c:28: error: for each function it appears in.)
    strrev.c:28: error: ‘head’ undeclared (first use in this function)
    strrev.c:34: warning: passing argument 1 of ‘strcpy’ from incompatible pointer type
   /usr/include/string.h:128: note: expected ‘char * __restrict__’ but argument is of         type ‘char **’
Run Code Online (Sandbox Code Playgroud)

主要代码:

int main(int argc, char *argv[])
{
    if (argc != 3) {
        fprintf(stderr, "usage: intrev <input file> <output file>\n");
        exit(1);
    }

    FILE *fp = fopen(argv[1], "r");
    assert(fp != NULL);


    Node *head = malloc(sizeof(Node));
    head->ptr=NULL;

    char str[15];
    while (fgets(str, 15, fp) != NULL){
        struct Node *currNode = malloc(sizeof(Node));
        strcpy(currNode->info, str);
        currNode->ptr = head;
        head=currNode;
    }

    char *outfile = argv[2];
    FILE *outfilestr = fopen(outfile, "w");
    assert(fp != NULL);

    while (head->ptr != NULL){
        fprintf(outfilestr, "%s\n", head->info);
        head = head->ptr;
    }

    fclose(fp);
    fclose(outfilestr);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

Som*_*ude 5

typedef的结构语法错误.您需要在结构定义之后放置typedef名称:

typedef struct Node  /* <- structure name */
{
    /* ... */
} Node;  /* <- typedef name */
Run Code Online (Sandbox Code Playgroud)

并且可以对结构和类型使用相同的名称,因为它们都存在于不同的命名空间中.