【问题标题】:Program produce Wrong output程序产生错误的输出
【发布时间】:2013-05-28 04:50:41
【问题描述】:

我写了一个关于队列和动态内存分配的程序。这就是我的程序需要做的——将值插入队列并将其从队列中删除;就这么简单。 但我的问题是它只打印了分配值的变量的名称,而程序没有响应。

这是我的程序:

#include <stdio.h>
#define MAX 180

struct cakes{
        int spongecake;
        int meringue;
        int chocalate;
        int red_velvet;
        struct newcake *next;
};

struct Queue{
        int front;
        int rear;
        int count;
        int cake[10];
};

void init(struct Queue *);
int isFull(struct Queue *);
void insert(struct Queue *,int);
int isEmpty(struct Queue *);
int removes(struct Queue *);

void cake_order(struct cakes *);
void order_out(struct cakes *);

main()
{
        struct cakes *head;

        head=(struct cakes *)malloc(sizeof(struct cakes ));
        cake_order(&head); //this is a seperate function and it works perfectly
        head->next=(struct cakes *)malloc(sizeof(struct cakes));
        order_out(&head->next);
}
void init(struct Queue *q)
{
        q->front=0;
        q->rear=10-1;
        q->count=0;
}

int isFull(struct Queue *q)
{
        if(q->count==10)
        {
                return 1;
        }
        else 
        {
                return 0;
        }
}

void insert(struct Queue *q,int x)
{
        if(!isFull(q))
        {
                q->rear=(q->rear+1)%10;
                q->cake[q->rear]=x;
                q->count++;
        }

}

int isEmpty(struct Queue *q)
{
        if(q->count==0)
        {
                return 1;
        }
        else
        {
                return 0;
        }
}

int removes(struct Queue *q)
{
        int caked=NULL;

        if(!isEmpty(q))
        {
                caked=q->cake[q->front];
                q->front=(q->front+1)%10;
                q->count--;
                return caked;
        }
}

void order_out(struct cakes *order)
{
        struct Queue s;
        int i;  

        order->spongecake=20;
        order->meringue=75;
        order->chocalate=40;
        order->red_velvet=30;

        init(&s);

        for(i=0;i<10;i++)
        {
                insert(&s,order->chocalate);
                insert(&s,order->spongecake);
                insert(&s,order->meringue);
                insert(&s,order->red_velvet);
    }

        while(!isEmpty(&s)) 
        {   
                printf("%d",removes(&s));
        }
}

这里似乎有什么问题? 我是 C 新手,所以在用这种语言进行调试时有点慢。

感谢您的宝贵时间。

这是输出:

【问题讨论】:

  • struct newcaketypedef 吗?
  • 它是一个指向新结构的指针
  • 那为什么是head-&gt;next=(struct cakes *)malloc(sizeof(struct cakes));而不是head-&gt;next=(struct newcake*)malloc(sizeof(struct newcake));??你的意思是struct cakes *next 而不是struct newcake *next??
  • newcake 是否意味着与 cake 不同?因为正如 raj raj 指出的那样,也许您的意思是 struct cakes *next。除非有另一个名为 newcake 的结构,其定义不存在。
  • 另外 cake_order(&amp;head); 实际上应该是 cake_order(head); 因为 head 已经是一个指针(即内存中的地址)。在这种情况下传递地址的地址没有任何意义。对我来说,这解决了分段错误问题。

标签: c queue dynamic-memory-allocation


【解决方案1】:

这里有很多问题,首先如果mainint main() 一样正确声明会更好,然后它在最后返回一个值,例如return 0;点赞:

int main()
{
    .... // code

    return 0; // normally 0 is returned if execution has been successful
}

代码似乎还有其他问题,因为我无法编译它,例如order_out() 末尾没有右括号(就在 while 循环之后)。

如果您提供cake_order() 函数也很好。

它也缺少包含 stdlib.h 和第 45 行 (head=(struct cakes *)malloc(sizeof(struct cakes ));) 我注意到你转换了 malloc 的结果 which is not necessary

如果我可以进一步补充,不要记得free() 分配给malloc() 的内存。我在您的代码中没有看到一条 free() 语句。

【讨论】:

  • 谢谢您的回复,即使在编辑之后,它仍然是一样的。
  • 我在代码中添加了一些其他建议,我建议您将它们考虑在内,因为至少它会使您的程序更可预测。我不知道您的编译器是否会发出任何警告,但我确定确实如此。最好也将这些考虑在内,因为编译的代码并不意味着它可以正常工作。
猜你喜欢
  • 1970-01-01
  • 2023-03-22
  • 2016-11-12
  • 1970-01-01
  • 2016-02-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-24
  • 2019-11-23
相关资源
最近更新 更多