5

I have two headers files, stack.h

#ifndef __STACK_H__
#define __STACK_H__



typedef struct StackElement_
{
  int value;
  struct StackElement_ *next;
} StackElement, *Stack;


/*----------------------Prototypes----------------------*/

Stack new_stack();

void push_to_stack(Stack *, int);
int pop_stack(Stack *);
int peek_stack(Stack);

int stack_length(Stack);
void print_stack(Stack);
Bool is_empty_stack(Stack);

void clear_stack(Stack);

/*------------------------------------------------------*/



#endif

and utils.h

#ifndef __UTILS_H__
#define __UTILS_H__



#define INT_MIN -2147483648

/*------------------------Typedef-----------------------*/

typedef enum Boolean_ {
  FALSE,
  TRUE
} Bool;

/*------------------------------------------------------*/



#endif

In stack.h, I need to know Bool but when I include utils.h in stack.c, the structure is still not known in stack.h. How to do this without having to define it directly in stack.h?

1
  • I had the exact same question for such a long time. Glad I found yours. The answers are excellent. My problem got resolved within seconds. Really grateful!!!! Commented Jul 19, 2022 at 6:54

2 Answers 2

3

You need to include utils.h in stack .h (not stack.c). The #include statements are part of the C macro language, which is a simple pre-processor. It literally takes the file given as the filename (#include filename) and inserts it into your program (during the pre-processor stage of the compile).

Sign up to request clarification or add additional context in comments.

Comments

2

A source file or include file should include all files necessary.

So if stack.h is dependent on declarations in util.h, then you should #include "util.h" in stack.h. That way, any module that includes stack.h doesn't need to worry about also including util.h and putting it in the right order.

If a module decides to include both that should be fine as well. The include guards you've added should address that.

You didn't show the source of stack.c, but you most likely included stack.h before util.h. That would explain your issue since the definitions needed by stack.h appear after them instead of before.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.