C语言语法错误

Syntax errors in C language

我是来自 python 的开发人员。我有一个 C 头文件,但是当我尝试使用它时,我遇到了很多语法错误。

我正在使用 MSVC 编译器。

代码如下:

stack.h

typedef struct stack
{
    void* data;
    Stack* next;
} Stack;

Stack* _stack_init(Stack* s, void* item);
void _stack_push(Stack* s, void* item);
void _stack_pop(Stack* s);

stack.c:

#include <stdlib.h>

typedef struct stack
{
    void* data;
    struct stack* next;
} Stack;

Stack* _stack_init(Stack* s, void* item) {
    Stack* new_stack = (Stack*) malloc((sizeof(Stack)));
    new_stack->data = &item;
}

void _stack_push(Stack* s, void* item) {
    if (s->next != NULL) {
        _stack_push(s->next, item);
    }
    Stack* new_item = _stack_init(s, item);
    s->next = new_item;
}

void _stack_pop(Stack* s) {
    if (s->next == NULL) return s;
    else _stack_pop(s->next);
}

这里是错误:

g:\progs\c\csv_reader\libs/structures/stack.h(4): error C2061: syntax error: identifier 'Stack'
g:\progs\c\csv_reader\libs/structures/stack.h(5): error C2059: syntax error: '}'
g:\progs\c\csv_reader\libs/structures/stack.h(7): error C2143: syntax error: missing '{' before '*'
g:\progs\c\csv_reader\libs/structures/stack.h(7): error C2143: syntax error: missing ')' before '*'
g:\progs\c\csv_reader\libs/structures/stack.h(7): error C2059: syntax error: 'type'
g:\progs\c\csv_reader\libs/structures/stack.h(7): error C2059: syntax error: ')'
g:\progs\c\csv_reader\libs/structures/stack.h(8): error C2143: syntax error: missing ')' before '*'
g:\progs\c\csv_reader\libs/structures/stack.h(8): error C2143: syntax error: missing '{' before '*'
g:\progs\c\csv_reader\libs/structures/stack.h(8): error C2059: syntax error: 'type'
g:\progs\c\csv_reader\libs/structures/stack.h(8): error C2059: syntax error: ')'
g:\progs\c\csv_reader\libs/structures/stack.h(9): error C2143: syntax error: missing ')' before '*'
g:\progs\c\csv_reader\libs/structures/stack.h(9): error C2143: syntax error: missing '{' before '*'
g:\progs\c\csv_reader\libs/structures/stack.h(9): error C2059: syntax error: ')'

我个人看不出任何语法错误。谁能向我解释这个错误? 提前感谢您的帮助!

typedef struct stack
{
    void* data;
    Stack* next; // 'Stack' is unknown by the compiler
} Stack;

应该是

typedef struct stack
{
    void* data;
    struct stack* next;
} Stack;

typedef struct stack Stack;

struct stack
{
    void* data;
    Stack* next;
};

但是你为什么要在 .c 文件中重新定义结构?

此外,您忘记return新分配的指针:

Stack* _stack_init(Stack* s, void* item) { // You don't need 's' 
    Stack* new_stack = (Stack*) malloc((sizeof(Stack)));
    new_stack->data = &item;  // You don't want the address of, remove the '&'
    return new_stack; // --> here
}

这里正好相反,你 return 在 void 函数中:

void _stack_pop(Stack* s) {
    if (s->next == NULL) return s;
#include <stdlib.h>

struct stack
{
    void* data;
    struct stack* next;
};

typedef struct stack Stack; // use typedef after creating struct


Stack* _stack_init(Stack* s, void* item);
void _stack_push(Stack* s, void* item);
void _stack_pop(Stack* s);

Stack* _stack_init(Stack* s, void* item) {
    Stack* new_stack = (Stack*) malloc((sizeof(Stack)));
    new_stack->data = &item;
    return new_stack;
}

void _stack_push(Stack* s, void* item) {
    if (s->next != NULL) {
        _stack_push(s->next, item);
    }
    Stack* new_item = _stack_init(s, item);
    s->next = new_item;
}

void _stack_pop(stack* s) {
    if (s->next == NULL) return;
    else _stack_pop(s->next);
}