【问题标题】:Syntax errors in C language [duplicate]C语言中的语法错误[重复]
【发布时间】:2021-01-07 07:29:57
【问题描述】:

我是一名来自 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: ')'

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

【问题讨论】:

  • 有很多问题...您甚至没有在您的stack.c 文件中包含stack.h。当您遇到一堆错误时,只需修复第一个错误:在您的情况下,您使用的类型 (Stack) 尚不为人所知。使用前向声明。
  • 天啊!我是新手。也许我应该先回去掌握基础知识。与 python 相比,C 有很多陷阱????

标签: c syntax syntax-error


【解决方案1】:
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 文件中的结构呢?

另外,您忘记返回新分配的指针:

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
}

相反,你在 void 函数中返回一些东西:

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

【讨论】:

    【解决方案2】:
    #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);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-11
      • 2012-12-13
      • 2021-11-04
      • 2018-03-10
      • 2015-09-17
      • 2017-11-14
      • 2016-12-01
      • 1970-01-01
      相关资源
      最近更新 更多