asso
asso

Reputation: 3

"private" variable in C header file

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).

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

Answers (2)

John Zwinck
John Zwinck

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

Some programmer dude
Some programmer dude

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

Related Questions