0
votes

the implementation level of the array-based

#include "stack.h"

void creat_stack(Stack *s) {
    s->Top = 0;
}
int isFull(Stack s) {
    return (s.Top == Max ? 1 : 0);
}
int isEmpty(Stack s) {
    return (s.Top == Max ? 1 : 0);
}
void push(stack_entry e, Stack *s) {
    if (!isFull(*s))
        s->entry[s->Top++] = e;
    else
        printf("Error : Stack Overflow\n");
}
void pop(stack_entry *e,Stack *s) {
    if(!isEmpty(*s))
        *e = s->entry[s->Top--];
    else
        printf("Error : Stack Underflow\n");
}

the header file stack.h that consists of the prototypes of the functions along with the definition of the stack element type stack_entry.

#pragma once
#include <stdio.h>

#define Max 10
typedef char stack_entry;

typedef struct Stack{
    int Top;
    stack_entry entry[Max];
}Stack;

void creat_stack(Stack *s);
int isFull(Stack s);
int isEmpty(Stack s);
void push(stack_entry e,Stack *s);
void pop(stack_entry *e, Stack *s);

so my question why i have to include the header file "stack.h" in the "stack.c" file ??

2
For the typedef, define, and struct definition. - user975989
And to make sure the declarations of the functions and their implementations match. - R Sahu
Try to compile without the include and your compiler will tell you :) - Joël Hecht

2 Answers

0
votes

You should have to add the stack.h because this file is it who has all declarations of your typedef define struct and methods

So without then, will be impossible to access any of those informations.

It is the same if you declare a method below the main function and do not declare his signature upstairs the main, you cannot access..

So, all this happens with all include files that you use at the top of your file..

Do not forget C is a sequential language, so you will never know what do you have in the next line if you don't tell the compiler.

check more here

0
votes

First, don't include "stdio.h" in the header file, you aren't use it there anyway. include it in the source file where you are using it. You need to include the header file for the "struct stack" and the "define".