Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/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_Queue_Dynamic Memory Allocation - Fatal编程技术网

C 程序产生错误的输出

C 程序产生错误的输出,c,queue,dynamic-memory-allocation,C,Queue,Dynamic Memory Allocation,我写了一个关于队列和动态内存分配的程序。这就是我的程序需要做的事情——在队列中插入值并将其从队列中删除;那很简单。 但我的问题是,它只打印赋值变量的名称,而程序没有响应 这是我的节目: #include <stdio.h> #define MAX 180 struct cakes{ int spongecake; int meringue; int chocalate; int red_velvet;

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

这是我的节目:

#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));
        }
}
#包括
#定义最大值180
结构蛋糕{
海绵蛋糕;
蛋白酥皮;
内巧克力;
内红色天鹅绒;
结构newcake*next;
};
结构队列{
内锋;
内部后部;
整数计数;
int-cake[10];
};
void init(结构队列*);
int isFull(结构队列*);
void insert(结构队列*,int);
int isEmpty(结构队列*);
int删除(结构队列*);
取消蛋糕订单(结构蛋糕*);
无效订单(结构蛋糕*);
main()
{
结构蛋糕*头;
head=(结构蛋糕*)malloc(结构蛋糕的尺寸);
cake_order(&head);//这是一个单独的函数,它工作得非常好
head->next=(结构蛋糕*)malloc(sizeof(结构蛋糕));
下单(&head->next);
}
void init(结构队列*q)
{
q->front=0;
q->后=10-1;
q->count=0;
}
int isFull(结构队列*q)
{
如果(q->count==10)
{
返回1;
}
其他的
{
返回0;
}
}
空插入(结构队列*q,整数x)
{
如果(!isFull(q))
{
q->rear=(q->rear+1)%10;
q->蛋糕[q->后]=x;
q->count++;
}
}
int isEmpty(结构队列*q)
{
如果(q->count==0)
{
返回1;
}
其他的
{
返回0;
}
}
int删除(结构队列*q)
{
int-caked=NULL;
如果(!isEmpty(q))
{
结块=q->蛋糕[q->正面];
q->front=(q->front+1)%10;
q->计数--;
返结块;
}
}
作废订单(结构蛋糕*订单)
{
结构队列;
int i;
订单->海绵蛋糕=20;
订单->蛋白酥=75;
订单->巧克力=40;
订单->红色天鹅绒=30;
初始化(&s);
对于(i=0;ichocalate);
插入(&s,订单->海绵蛋糕);
插入(&s,订单->蛋白酥);
插入(&s,订单->红色天鹅绒);
}
而(!isEmpty(&s))
{   
printf(“%d”,删除(&s));
}
}
这里有什么问题吗? 我是C语言新手,所以在用这种语言调试时,我的速度有点慢

谢谢你抽出时间

以下是输出:


这里有很多问题,首先最好像
int main()
那样正确声明
main
,然后在末尾返回一个值,例如
返回0如:

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

    return 0; // normally 0 is returned if execution has been successful
}
代码似乎还有其他问题,因为我无法编译它,例如,
order\u out()
(就在while循环之后)的末尾没有右括号

如果您提供了
cake\u order()
函数也很好

它还缺少了stdlib.h的include,在第45行(
head=(struct-cakes*)malloc(sizeof(struct-cakes));
)我注意到您已对malloc的结果进行了转换


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

这里有很多问题,首先最好像
int main()
那样正确声明
main
,然后在结尾返回一个值,例如
返回0如:

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

    return 0; // normally 0 is returned if execution has been successful
}
代码似乎还有其他问题,因为我无法编译它,例如,
order\u out()
(就在while循环之后)的末尾没有右括号

如果您提供了
cake\u order()
函数也很好

它还缺少了stdlib.h的include,在第45行(
head=(struct-cakes*)malloc(sizeof(struct-cakes));
)我注意到您已对malloc的结果进行了转换


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

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