C 这个代码怎么了?

C 这个代码怎么了?,c,data-structures,C,Data Structures,我需要以下代码的帮助 typedef struct orders { int quantity; char foodname[50]; } ORDER; ORDER *ptr; typedef struct Table { int tableno; int priority; ORDER *orders; struct Table *next; } TABLE; TABLE *head, *s; int n = 0; int insert(

我需要以下代码的帮助

typedef struct orders
{
    int quantity;
    char foodname[50];
} ORDER;

ORDER *ptr;

typedef struct Table
{
    int tableno;
    int priority;
    ORDER *orders;
    struct Table *next;
} TABLE;

TABLE *head, *s;
int n = 0;

int insert(int tablenum, int prio, char foodname[], int qty)
{
    TABLE *newO, *temp, *temp2;
    newO = (TABLE*)malloc(sizeof(TABLE));
    newO->tableno = tablenum;
    newO->priority = prio;
    strcpy(newO->orders->foodname, foodname);
    newO->orders->quantity = qty;
       //more code here...
}
在该程序的主要功能中,用户将被询问要订购的食物的表号、优先顺序号、名称和数量

这段代码中还有一个showlist函数,它将从最高优先级到最低优先级打印列表上的所有数据

现在我的问题是,例如,我已经有两个不同的事务,发生的是我的第二个事务复制了我第一个事务的“foodname”和“quantity”

请帮帮我,伙计们

TABLE *newO = (TABLE*)malloc(sizeof(TABLE));
分配内存,但不为
订单
分配内存,在调用
malloc
之后,它只是一个未初始化的指针,因此此行:

newO->orders->quantity = qty;
调用未定义的行为。您还需要为订单分配内存,例如:

TABLE *newO = (TABLE*)malloc(sizeof(TABLE));
newO->orders = (ORDER*)malloc(10*sizeof(ORDER)); 
...
newO->orders[0]->quantity = qty;

…虽然老实说,
订单
是否真的应该是一个数组很难说。

表格->订单
应该是指向单个
订单
条目的指针还是元素数组?此外,您的结构和标记命名不一致,这将导致混淆。@haccks:他还没有投票的特权:)