Error 'is_empty' was not declared in this scope.why does it give error?

Error 'is_empty' was not declared in this scope.why does it give error?

Problem Description:

output=[Error] ‘is_empty’ was not declared in this scope
must be:
Example
Input string : abbccbaabccbba message will be The string is valid
aaabbcb
bcbaab message will be The string is invalid
aadbxcy*ycxbdaa message will be Wrong character!!!
what should i do?

#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<string.h>

typedef struct
{
 char home[35];
 int top;
} My_stack;

void push(My_stack * s, char c) // push (insert) operation
{ // assume there is enough space for pushing next element!
    s -> top ++;
    s -> home[s -> top] = c;
}

int pop(My_stack * s) // pop (remove) operation
{
    if(is_empty (*s)) {
        printf("ERROR: Nothing to pop - program terminatesn");
        exit(1);
    }
    return (s ->home[s ->top --]);
}

int is_empty(My_stack * s) // checking whether stack is empty or not
{
    return(s -> top < 0 ? 1 : 0);
}
    
int main(){
    char ch[25];
    int i,l;
    My_stack stack;
    printf("give the string");
    scanf("%s",ch);
    l=strlen(ch);
    i=0;    
    while(ch[i]!='') {
        if(ch[i]!='A'&&ch[i]!='B'&& ch[i]!='*') {
            printf("the string is not accepted allowed caracters are A,B and * ");
            exit(0);
        }
        i++;
    }   
    i=0;    
    while(ch[i] != '*') {
        push(&stack, ch[i]);
        i++;
    }
    i++; // one step forward to pass '*
    while(ch[i] != '') {
        if(ch[i] != pop(&stack)) {
            printf("the string is not valid");
            exit(0);
        }
        i++;
    }
    printf("the string is valid");          
    return 0;
}

Solution – 1

First of all, you need to pass the pointer to the function, not the actual value, so your functioncall must be changed to:

is_empty (s)

instead of

is_empty (*s)

Then, the reason is because you define your function is_empty after your first use. To prevent this error, you should declare your function first. This tells the compiler "hey, there exists this function with this signature, the definition is given later though" so then you can use it in your program before having implemented it explicitly.

#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<string.h>

typedef struct
{
 char home[35];
 int top;
} My_stack;

int is_empty(My_stack * s); // <----- DECLARATION OF FUNCTION

int pop(My_stack * s) // pop (remove) operation
{
    if(is_empty (s)) {
        printf("ERROR: Nothing to pop - program terminatesn");
        exit(1);
    }
    return (s ->home[s ->top --]);
}

int is_empty(My_stack * s) // checking whether stack is empty or not
{
    return(s -> top < 0 ? 1 : 0);
}

Or, you put the entire definition before your first use so then there’s no need for a declaration:

#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<string.h>

typedef struct
{
 char home[35];
 int top;
} My_stack;

int is_empty(My_stack * s) // checking whether stack is empty or not
{
    return(s -> top < 0 ? 1 : 0);
}

int pop(My_stack * s) // pop (remove) operation
{
    if(is_empty (s)) {
        printf("ERROR: Nothing to pop - program terminatesn");
        exit(1);
    }
    return (s ->home[s ->top --]);
}
Rate this post
We use cookies in order to give you the best possible experience on our website. By continuing to use this site, you agree to our use of cookies.
Accept
Reject