0

I'm trying to implement a stack using array in #c. I have created the implementation and the header files, then linked the stack C library from linker setting. The structure of the stack is included only in the header file stack.h.

#define max_stack 100

Typedef int stackentry ;

typedef struct stack

{int top; stackentry entry[max_stack] ;} STack;

I include the stack.h in both main.c and stack.c but the problem is When I first build and run its okay but with any modification in the stack size through changing max_stack in the header file it doesn't affect the output That was clear when I make

#define max_stack 5

Then using push function more than five times while taking into consideration the full stack mechanism test before pushing and it pushes without looking to the array size which is 5 in that case. The compiler only sees it as 100 which is first declared.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • 1
    How do you build your programs? If you build manually in a console there's no dependency handling, you have to add that yourself (for example by using a Makefile). – Some programmer dude Jun 04 '20 at 17:04
  • 3
    If you use some makefile to build your program, you need to add a dependency to the header. – Gerhardh Jun 04 '20 at 17:06
  • 2
    The code you have shown is not compiled at all in your setup. That is the answer. I know because any compiler would scream at you when trying to compile that. I conlcude that you did not include that header into any compiled code source file. Since you say that you do include that header into two code files it follows logically that those two files are not compiled. Therefor much more information is needed on how exactly you build. Please make a [mre] which includes your build setup. I.e. add a makefile or similar. – Yunnosch Jun 04 '20 at 17:10
  • Did you tell your build process that the `stack.o` and `main.o` files are out of date if the `stack.h` header is newer than the object file? Programs like `make` won't infer the dependency — you have to state it. (Most programs like `make` will automatically infer that `main.o` depends on `main.c` — you probably don't need to say that explicitly.) – Jonathan Leffler Jun 04 '20 at 17:27
  • I do understand ur point of view , so how can i state the dependency to the header file? Im using codeblocks as ide – Ammar anwur Jun 05 '20 at 01:06
  • As far as I understand CodeBlocks should be handling header file dependencies for you (IIRC it uses `make` under the hood). Is your header file listed in your CodeBlocks project? Are both source files? – Some programmer dude Jun 05 '20 at 04:23
  • Thanx i will check that – Ammar anwur Jun 05 '20 at 14:46

0 Answers0