【问题标题】:What is the error in the code? [closed]代码中的错误是什么? [关闭]
【发布时间】:2017-04-03 23:06:10
【问题描述】:

在下面的函数中。我尝试使用堆栈并使用数组实现。我制作了 create stack 、 push 、 pop 等函数。但是在编译时显示错误。请帮我找出问题所在?

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

【问题讨论】:

  • 错误和行号是什么?
  • 可能不相关,但你不应该投malloc
  • typedef 隐藏指针容易出错,被认为是不好的做法,使代码更难阅读......

标签: c stack


【解决方案1】:

您在 printstack 函数的末尾缺少},并且在代码末尾有一个额外的}。添加缺少的一项并删除多余的一项,它将编译。此外,您的主函数应该返回 int(int main() 而不是 void main()),并且您应该从主函数返回 return 0;

【讨论】:

  • 我知道返回 0;最后没有必要。即使在这样做之后(你告诉我的)它也显示流行;断言函数失败。
  • 那是因为您的 printstack 函数有效地清空了您的堆栈,所以 s-&gt;top 设置为 -1。断言失败,因为s-&gt;top 不大于0。即使你的程序会用void main()编译,你也不应该这样做,正确的方法是int main()int main(int argc, char *argv[])
【解决方案2】:

你的代码确实有问题:

  • printstack 的函数定义末尾缺少 }

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

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

  • main 的原型应该是 int main(void)int main(int argc, char *argv[]) 或等效的。在 C99 及更高版本中,在 main() 末尾返回 0 是可选的,但被认为是好的样式。

  • 强制转换malloc() 的返回值是不必要的,如果您省略包含&lt;stdlib.h&gt;,可能会隐藏一些问题,请不要在C 代码中使用它。

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

  • printstack 不应该修改stack,它应该使用局部变量:

    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;
}

【讨论】:

  • 先生,它没有显示整数我该怎么办? @chqrlie
  • @pottersher:我通过一些修复更新了答案,并使用 int 作为元素类型以使堆栈内容可打印。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-11-16
  • 1970-01-01
  • 2019-11-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-07-05
相关资源
最近更新 更多