Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 代码中的错误是什么?_C_Stack - Fatal编程技术网

C 代码中的错误是什么?

C 代码中的错误是什么?,c,stack,C,Stack,在下面的函数中。我尝试使用堆栈并使用数组实现。我创建了堆栈、推送、弹出和函数。但在编译时,它显示了一个错误。请帮我找出问题出在哪里 #include <stdio.h> #include <assert.h> #include <stdlib.h> struct stack_struct { char a[100]; int top; }; typedef struct stack_struct *stack; stack charc;

在下面的函数中。我尝试使用堆栈并使用数组实现。我创建了堆栈、推送、弹出和函数。但在编译时,它显示了一个错误。请帮我找出问题出在哪里

#include <stdio.h>
#include <assert.h>
#include <stdlib.h>

struct stack_struct {
    char a[100];
    int top;
};

typedef struct stack_struct *stack;

stack charc;

stack createstack() {
    stack s = (stack) malloc(sizeof(struct stack_struct));
    s->top = -1;//initialize the stack
    return s;
}

void push(stack s, char x) {
    s->top++;
    s->a[s->top] = x;
}

char pop(stack s) {
    assert(s->top > 0);
    char x;
    x = s->a[s->top];
    s->top--;
    return x;
}

void printstack(stack s) {
    while (s->top != -1) {
        printf("%c", s->a[s->top]);
        s-> top--;
    }


void main() {
    charc = createstack();      
    push(charc, 3);
    printstack(charc);
    pop(charc);
    printstack(charc);
    push(charc, 4);
    printstack(charc);
    push(charc, 5);
    printstack(charc);
    push(charc, 6);
    printstack(charc);
    push(charc, 7);
    printstack(charc);
}
}
#包括
#包括
#包括
结构栈{
chara[100];
int top;
};
typedef struct stack_struct*stack;
堆栈charc;
stack createstack(){
堆栈s=(堆栈)malloc(sizeof(struct stack_struct));
s->top=-1;//初始化堆栈
返回s;
}
无效推送(堆栈s、字符x){
s->top++;
s->a[s->top]=x;
}
char pop(堆栈s){
断言(s->top>0);
字符x;
x=s->a[s->top];
s->顶部--;
返回x;
}
无效打印堆栈(堆栈s){
而(s->top!=-1){
printf(“%c”,s->a[s->top]);
s->顶部--;
}
void main(){
charc=createstack();
推(charc,3);
打印堆栈(charc);
pop(charc);
打印堆栈(charc);
推(charc,4);
打印堆栈(charc);
推(charc,5);
打印堆栈(charc);
推(charc,6);
打印堆栈(charc);
推(charc,7);
打印堆栈(charc);
}
}

在printstack函数的末尾缺少
}
,并且在代码末尾有一个额外的
}
。添加缺少的一个,删除多余的一个,它将编译。另外,main函数应该返回int(
intmain()
而不是
voidmain()
),并且应该
返回0从主功能中

您的代码中确实存在问题:

  • printstack

  • 由于文件末尾有一个额外的
    }
    ,在
    main()
    的定义之后,并且
    gcc
    允许本地函数定义,因此错误消息可能很难解释

  • pop
    中的断言应为
    assert(s->top>=0)因为
    s->top==0
    表示存在一个元素的堆栈

  • main
    的原型应为
    intmain(void)
    intmain(intargc,char*argv[])
    或同等产品。在C99和更高版本中,在
    main()
    的末尾返回
    0
    是可选的,但被认为是好的样式

  • 强制转换
    malloc()
    的返回值是不必要的,如果忽略了
    ,则可能会隐藏一些问题,不要在C代码中使用它

  • 不建议将指针隐藏在
    typedef
    s后面,这会使代码更难阅读,并经常导致编程错误。
    堆栈
    是对象,不是指向对象的指针。使用隐式指针会使差异不那么明显,并造成混淆

  • printstack
    不应修改
    堆栈,它应使用局部变量:

    void printstack(const struct stack_struct *s) {
        for (int i = s->top; i >= 0; i--) {
            printf("%c", s->a[i]);
        }
        printf("\n");
    }
    
  • 您的代码不需要使用全局变量,请在退出程序之前将
    charc
    设置为
    main
    函数的本地变量,并将其设置为
    free

  • 堆栈元素类型应该是
    int
    ,而不是
    char
    ,因为要将数字推送到堆栈上

以下是一个简化版本:

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>

typedef struct stack {
    int a[100];
    int top;
} stack;

stack *createstack(void) {
    stack *s = malloc(sizeof(*s));
    s->top = -1; // initialize the stack as empty
    return s;
}

void push(stack *s, int x) {
    s->a[++s->top] = x;
}

int pop(stack *s) {
    assert(s->top >= 0);
    return s->a[s->top--];
}

void printstack(const stack *s) {
    for (int i = s->top; i >= 0; i--) {
        printf("%d ", s->a[i]);
    }
    printf("\n");
}

int main(void) {
    stack *st = createstack();

    push(st, 3);
    printstack(st);
    pop(st);
    printstack(st);
    push(st, 4);
    printstack(st);
    push(st, 5);
    printstack(st);
    push(st, 6);
    printstack(st);
    push(st, 7);
    printstack(st);

    free(st);
    return 0;
}
#包括
#包括
#包括
typedef结构堆栈{
INTA[100];
int top;
}堆叠;
堆栈*createstack(无效){
堆栈*s=malloc(sizeof(*s));
s->top=-1;//将堆栈初始化为空
返回s;
}
无效推送(堆栈*s,整数x){
s->a[++s->top]=x;
}
int-pop(堆栈*s){
断言(s->top>=0);
返回s->a[s->top--];
}
无效打印堆栈(常量堆栈*s){
对于(int i=s->top;i>=0;i--){
printf(“%d”,s->a[i]);
}
printf(“\n”);
}
内部主(空){
stack*st=createstack();
推(st,3);
打印堆栈(st);
流行音乐(st),;
打印堆栈(st);
推(st,4);
打印堆栈(st);
推(st,5);
打印堆栈(st);
推(st,6);
打印堆栈(st);
推(st,7);
打印堆栈(st);
免费(st);
返回0;
}

错误和行号是什么?可能不相关,但您不应该使用
typedef
隐藏指针
typedef
容易出错,被认为是错误做法,使代码更难阅读…我已经了解到返回0;最后没有必要。而且,即使这样做了(你告诉我的),它也显示出流行音乐;断言函数失败。这是因为printstack函数有效地清空了堆栈,所以
s->top
被设置为-1。断言失败,因为
s->top
不大于
0
。即使您的程序将使用
void main()
进行编译,您也不应该这样做,正确的方法是
int main()
int main(int argc,char*argv[])
。先生,它没有显示整数,我该怎么办@chqrlie@pottersher:我用一些修正更新了答案,并使用
int
作为元素类型,以使堆栈内容可打印。