0

I am a developer coming from python. I have a C header file but when I try to use it, I get a lot of syntax errors.

I am using MSVC compiler.

here is the code:

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);
}

and here is the error:

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: ')'

I can't see any syntax errors personally. can anyone explain to me about this error? Thanks for your help in advance!

A.Mohammadi
  • 304
  • 4
  • 16
  • 2
    There are a number of issues... you are not even including `stack.h` in your `stack.c` file. When you have a bunch of errors just fix the first one: in your case you are using a type (`Stack`) that is not yet known. Use forward declarations. – ntd Jan 07 '21 at 07:46
  • Ohh my god!! I'm so newbie. Maybe I should get back and grasp the basics first. C has many traps comparing to python – A.Mohammadi Jan 07 '21 at 08:36

2 Answers2

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

should be

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

or

typedef struct stack Stack;

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

But why are you redefining the structure in the .c file?

Also, you forget to return the new allocated pointer:

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
}

and here the opposite, you return something in a void function:

void _stack_pop(Stack* s) {
    if (s->next == NULL) return s;
David Ranieri
  • 39,972
  • 7
  • 52
  • 94
0
#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);
}
Kanony
  • 509
  • 2
  • 12