Return 用户定义结构到动态数组元素
Return user defined structure to dynamic array element
我正在尝试创建一个动态数组来在数组的每个元素中存储一个链表。所以我定义链表结构如下:
//data type for adjacent bus stop
typedef struct AdjStopNode
{
int distance; //travel distance from the bus original stop to this adjcent stop
int stopID;
struct AdjStopNode *prev; //pointer to previous bus stop
struct AdjStopNode *next; //pointer to next bus stop
} AdjStopNode;
AdjStopNode *newAdjStopNode(int distance, int stopID)
{
AdjStopNode *newNode = (AdjStopNode *)malloc(sizeof(AdjStopNode));
assert(newNode != NULL);
newNode->distance = distance;
newNode->stopID = stopID;
newNode->next = NULL;
return newNode;
}
typedef struct AdjStopList
{
char stopname[20];
int numOfAdjStp;
struct BusAtStopList *buslist;
struct AdjStopNode *first; //pointed at the first AdjBusStop of the linked list
struct AdjStopNode *last; //pointed at the first AdjBusStop of the linked list
} AdjStopList;
AdjStopList *newAdjStopList()
{
AdjStopList *newList = (AdjStopList *)malloc(sizeof(AdjStopList));
newList->buslist = newBusAtStopList();
assert(newList != NULL);
memset(newList, NULL, 20 * sizeof(newList[0]));
newList->first = NULL;
newList->last = NULL;
newList->numOfAdjStp = 0;
return newList;
}
然后我定义了一个动态数组来存储每个AdjStopList
作为数组的一个元素如下:
typedef struct BusNetwork
{
int nBusStop; //number of bus stops in the newwork
struct AdjStopList *array;
} BusNetwork;
我为数组的每个元素分配一个空 AdjStopList
的函数如下:
//n is the number of AdjStopList
void listToArray(int n)
{
BusNetwork *newBN;
newBN = malloc(sizeof(BusNetwork));
assert(newBN != NULL);
newBN->nBusStop = n;
newBN->array = malloc(n * sizeof(AdjStopList)); //create an array of n number of dejacency lists
for (int i = 0; i < n; i++)
{
newBN->array[i] = newAdjStopList();
}
}
上面的代码在 newBN->array[i] = newAdjStopList()
处给我的错误是
a value of type "AdjStopList *" cannot be assigned to an
entity of type "struct AdjStopList" C/C++(513)
使用 VScode.
有人可以帮我解决这个问题并向我解释原因吗?非常感谢。
newBN->array
的类型是 struct AdjStopList *
所以 newBN->array[i]
的类型是 struct AdjStopList
但 newAdjStopList()
的 return 类型是 struct AdjStopList*
。因此,这应该可以解释您在
行中将 struct AdjStopList*
分配给 struct AdjStopList
时看到的错误
newBN->array[i] = newAdjStopList();
我相信你应该改变
typedef struct BusNetwork
{
int nBusStop; //number of bus stops in the newwork
struct AdjStopList *array;
} BusNetwork;
到
typedef struct BusNetwork
{
int nBusStop; //number of bus stops in the newwork
struct AdjStopList **array;
} BusNetwork;
因此 array
成为 指向 struct AdjStopList
的指针的数组。那么原始作业应该可以工作。
我正在尝试创建一个动态数组来在数组的每个元素中存储一个链表。所以我定义链表结构如下:
//data type for adjacent bus stop
typedef struct AdjStopNode
{
int distance; //travel distance from the bus original stop to this adjcent stop
int stopID;
struct AdjStopNode *prev; //pointer to previous bus stop
struct AdjStopNode *next; //pointer to next bus stop
} AdjStopNode;
AdjStopNode *newAdjStopNode(int distance, int stopID)
{
AdjStopNode *newNode = (AdjStopNode *)malloc(sizeof(AdjStopNode));
assert(newNode != NULL);
newNode->distance = distance;
newNode->stopID = stopID;
newNode->next = NULL;
return newNode;
}
typedef struct AdjStopList
{
char stopname[20];
int numOfAdjStp;
struct BusAtStopList *buslist;
struct AdjStopNode *first; //pointed at the first AdjBusStop of the linked list
struct AdjStopNode *last; //pointed at the first AdjBusStop of the linked list
} AdjStopList;
AdjStopList *newAdjStopList()
{
AdjStopList *newList = (AdjStopList *)malloc(sizeof(AdjStopList));
newList->buslist = newBusAtStopList();
assert(newList != NULL);
memset(newList, NULL, 20 * sizeof(newList[0]));
newList->first = NULL;
newList->last = NULL;
newList->numOfAdjStp = 0;
return newList;
}
然后我定义了一个动态数组来存储每个AdjStopList
作为数组的一个元素如下:
typedef struct BusNetwork
{
int nBusStop; //number of bus stops in the newwork
struct AdjStopList *array;
} BusNetwork;
我为数组的每个元素分配一个空 AdjStopList
的函数如下:
//n is the number of AdjStopList
void listToArray(int n)
{
BusNetwork *newBN;
newBN = malloc(sizeof(BusNetwork));
assert(newBN != NULL);
newBN->nBusStop = n;
newBN->array = malloc(n * sizeof(AdjStopList)); //create an array of n number of dejacency lists
for (int i = 0; i < n; i++)
{
newBN->array[i] = newAdjStopList();
}
}
上面的代码在 newBN->array[i] = newAdjStopList()
处给我的错误是
a value of type "AdjStopList *" cannot be assigned to an
entity of type "struct AdjStopList" C/C++(513)
使用 VScode.
有人可以帮我解决这个问题并向我解释原因吗?非常感谢。
newBN->array
的类型是 struct AdjStopList *
所以 newBN->array[i]
的类型是 struct AdjStopList
但 newAdjStopList()
的 return 类型是 struct AdjStopList*
。因此,这应该可以解释您在
struct AdjStopList*
分配给 struct AdjStopList
时看到的错误
newBN->array[i] = newAdjStopList();
我相信你应该改变
typedef struct BusNetwork
{
int nBusStop; //number of bus stops in the newwork
struct AdjStopList *array;
} BusNetwork;
到
typedef struct BusNetwork
{
int nBusStop; //number of bus stops in the newwork
struct AdjStopList **array;
} BusNetwork;
因此 array
成为 指向 struct AdjStopList
的指针的数组。那么原始作业应该可以工作。