【问题标题】:Error 'is_empty' was not declared in this scope.why does it give error?错误 \'is_empty\' 未在此范围内声明。为什么会报错?
【发布时间】:2022-12-06 22:52:00
【问题描述】:

output=[Error] 'is_empty' 未在此范围内声明 一定是: 例子 输入字符串:abbccbaabccbba 消息将是 The string is valid aaabbcbbcbaab 消息将是 The string is invalid aadbxcy*ycxbdaa 消息将是错误的字符!!! 我应该怎么办?

#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 terminates\n");
        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]!='\0') {
        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] != '\0') {
        if(ch[i] != pop(&stack)) {
            printf("the string is not valid");
            exit(0);
        }
        i++;
    }
    printf("the string is valid");          
    return 0;
}

【问题讨论】:

  • C 编译器从上到下读取。将 is_empty 的声明置于其首次使用点之上,或将定义向上移动至其首次使用点之上。

标签: c


【解决方案1】:

首先,您需要将指针传递给函数,而不是实际值,因此您的函数调用必须更改为:

is_empty (s)

代替

is_empty (*s)

然后,原因是因为您在第一次使用后定义了函数is_empty。为防止此错误,您应该宣布你的功能第一。这告诉编译器“嘿,这个函数存在这个签名,定义稍后给出”,这样你就可以在显式实现它之前在你的程序中使用它。

#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 terminates
");
        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);
}

或者,您将整个定义放在第一次使用之前,这样就不需要声明了:

#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 terminates
");
        exit(1);
    }
    return (s ->home[s ->top --]);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-10
    • 2015-06-23
    • 2010-10-02
    相关资源
    最近更新 更多