JiancongChen
JiancongChen

Reputation: 1

C - use the struct name as the parameter of function in strcut

I'm trying to implement stack in c. this is my code:

//header file
typedef struct
{
    ElementType Data[MaxSize];
    Position Top;
    
    int Push(SeqStack *L);
}SeqStack;

and this is what the compiler showed:

error: unknown type name 'SeqStack'

I'm not very familiar with c. What is the correct way to achieve such implementation?

Upvotes: 0

Views: 360

Answers (1)

Vlad from Moscow
Vlad from Moscow

Reputation: 311028

The name SeqStack is yet undefined in this line

int Push(SeqStack *L);

So the compiler issues an error.

Moreover you may not declare a function within a structure in C, You can declare a pointer to a function, Then when an object of the structure type will be defined you can initialize the pointer by some function.

You should write

typedef struct SeqStack
{
    ElementType Data[MaxSize];
    Position Top;
    
    int ( *Push )( struct SeqStack *L );
}SeqStack;

or

typedef struct SeqStack SeqStack;

struct SeqStack
{
    ElementType Data[MaxSize];
    Position Top;
    
    int ( *Push )( SeqStack *L );
};

Upvotes: 1

Related Questions