Reputation: 3
i am new to C programming and i need a C header file, which manages two stacks. There should only be two methods: push and pop (but they don't refer to the same stack).
push will push data onto the first stack which then gets popped and processed internally by the header functionality and the output is pushed (whenever) onto the second stack
pop will get you the output data from the second stack if there is any
The problem is, that i don't know how to make the second stack available for the pop method if there may not be any variables initialised in the header file. It seems so wrong to initialise the stack in the .c file.
Upvotes: 0
Views: 1732
Reputation: 249133
You don't need to expose the stacks in your header file at all. Just implement them entirely in your C implementation file; the header can be as simple as:
void push(int value);
int pop();
int empty(); // returns 0 if pop() is valid to call now
void process(); // do some work, moves things from the input stack to the output
In the implementation file, just initialize your stack pointers to NULL by default, and set up the stacks the first time the user calls push().
Upvotes: 2
Reputation: 409166
The header file could contain only prototypes for the push
and pop
functions. Then all data (variables etc.) are declared, defined and initialized in the source file where the push
and pop
functions are defined.
Upvotes: 3