0

I am trying to create a dynamic array to store a linked list in each element of the array. So I defined the linked list structure as below:

//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;
}

Then I defined a dynamic array to store each AdjStopList as an element of the array is as below:

typedef struct BusNetwork
{
    int nBusStop; //number of bus stops in the newwork
    struct AdjStopList *array;
} BusNetwork;

My function to assign an empty AdjStopList to every element of the array is as below:

//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();
    }
}

The above code gives me the error at newBN->array[i] = newAdjStopList() as

a value of type "AdjStopList *" cannot be assigned to an 
entity of type "struct AdjStopList" C/C++(513)

using VScode.

Could someone help me to fix this problem and explain to me why? Much appreciated.

5
  • The BusNetwork structure member array is an "array" of AdjStopList structure objects. The newAdjStopList function returns a pointer to a AdjStopList structure object. struct AdjStopList versus struct AdjStopList *. Commented Apr 19, 2021 at 11:37
  • 1
    OT: Your listToArray is wrong... newBN is never returned.... so all it does is to leak memory Commented Apr 19, 2021 at 11:49
  • OT: This memset(newList, NULL, 20 * sizeof(newList[0])); looks strange... what do you expect this to do? Commented Apr 19, 2021 at 11:51
  • I was trying to assign `NULL' to all the elements of the arrary, isn't it the right way? Sorry I am very new to programming Commented Apr 19, 2021 at 11:53
  • Thank you for highlighting this. Actually this code was partially finished....I paused when I had this issue. Commented Apr 19, 2021 at 11:55

1 Answer 1

1

The type of newBN->array is struct AdjStopList * so the type of newBN->array[i] is struct AdjStopList but the return type fromnewAdjStopList() is struct AdjStopList*. So that should explain the error that you see where you are assigning a struct AdjStopList* to struct AdjStopList in the line

newBN->array[i] = newAdjStopList();

I believe you should change

typedef struct BusNetwork
{
    int nBusStop; //number of bus stops in the newwork
    struct AdjStopList *array;
} BusNetwork;

to

typedef struct BusNetwork
{
    int nBusStop; //number of bus stops in the newwork
    struct AdjStopList **array;
} BusNetwork;

So that array becomes an array of pointers to struct AdjStopList. Then that original assignment should work.

Sign up to request clarification or add additional context in comments.

7 Comments

Thanks for the answer. It does fix the issue. So my understanding from you is that *array just creates a pointer to the dynamic array but not pointing to each element of the array? What if I wanted to create a static array to store `AdjStopList's?
That's right. Step back and simplify it as int * x. Here x could be made an array of ints. But x won't point to any element within the array (apart from the first element obviously). For referencing any element in the array, one would need x[i] to retrieve an int which is equivalent to *(x + i) i.e. the address pointed to by i blocks after x and then dereferencing it. Same is the case here.
By static array, you mean something like static struct AdjStopList[10]?
I see. I am having another issue since I changed it to struct AdjStopList *array . For example, if I wanted to assess stopname in one of the AdjStopList by saying strcpy(newBN->array[i].stopname, "test") gives me a similar error. Double pointer is very confusing to me still......
If you changed it to the double pointer version struct AdjStopList **array, then to access the stopname member, you'd need newBN->array[i]->stopname since now ``newBN->array[i]` is a pointer and not a concrete struct. Treat each level of pointer separately. You have struct AdjStopList **array. Dereferencing one level i.e. array[i] yields struct AdjStopList * (a pointer to struct AdjStopList). Now it's just like accessing a regular pointer. So you use the -> operator to access the member variables.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.