首页 > 解决方案 > 将指针数组传递给函数参数

问题描述

我正在尝试使用相邻链表图数据结构构建总线网络。一个简化的代码如下所示:

typedef struct BusNetwork
{
    struct AdjStopList *stopsArray;  //defing the array of pointers
} BusNetwork;

typedef struct Node
{
    int stopID;
    struct Node *next;
} Node;

typedef struct AdjStopList
{
    char stopName[20];
    int numOfAdjStp;
    struct Node *first;
} AdjStopList;

void insertStopAtLast(AdjStopList *L, int stopID)
{
    //add stopID to the last node of the list
    return;
}

void addBusRoute(AdjStopList *L[], int from, int to)
{
    if (from == to)
        return;

    insertStopAtLast(L[from], to);
    return;
}

void main(BusNetwork *BN, int from, int to)
{
    addBusRoute(BN->stopsArray, from, to);
}

问题在于addBusRoute(BN->stopsArray, from, to);似乎我没有传递与函数参数相同类型的值。但是我的理解BN->stopsArray是指针数组,应该和AdjStopList L[]. 什么地方出了错?

标签: cpointersgraphdynamic-arraysadjacency-list

解决方案


该参数AdjStopList *L[]与 具有相同的含义AdjStopList **L

另一方面,传递BN->stopsArray的是struct AdjStopList *.

参数是指向的指针 AdjStopList,但传递的是指向 的指针AdjStopList

因此,类型不同。


推荐阅读