【发布时间】:2020-09-09 09:29:42
【问题描述】:
我搜索了很多,我发现了类似的问题,但我仍然无法解决我的问题。 我想为指向表的指针数组分配内存(每个表都有自己的链表) 我希望我正确地解释了这个想法,这是代码:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct Meal
{
struct Meal* next;
char* productname;
int price;
int quantity;
}Meal, * PMeal;
typedef struct Table //each table is supposed to have its own linked list of meals
{
PMeal next;
PMeal prev;
int tableNumber;
int cheque;
}Table;
typedef struct allTables
{
int maxoftables;
Table** tarray;
int numberoftables;
}allTables;
这就是我尝试动态分配指向表的指针数组的方式:
(我认为这部分是正确的,它不会崩溃)
allTables tables;
tables.tarray = (Table**)malloc(sizeof(Table*) * tables.maxoftables)
注意:tables.maxoftables 是在调用 malloc 之前初始化的,是表的最大数量
这就是我尝试初始化每个表中的链表的方式:
(这是它告诉我“访问冲突写入位置”的地方)
for (i = 0; i < tables.maxoftables; i++)
{
(tables.tarray[i])->cheque = 0;
(tables.tarray[i])->next = NULL;
(tables.tarray[i])->prev = NULL;
(tables.tarray[i])->tableNumber = i + 1;
}
我相信我可以只分配一个结构表数组,但这是不允许的。
我希望您帮助我所需的一切都在这里并且得到了正确的解释
谢谢!
【问题讨论】:
标签: c arrays list pointers struct