I am using VScode as my IDE. So I created an array, and each element of the array stores a pointer to a linked list. My code is shown as below:
typedef struct AdjStopList
{
char stopName[20]; //name of stop bus is equal or less than 20 characters
int numOfAdjStp; //number of adjacent stops to this stop
struct BusAtStopList *buslist; // store all the buses passing this stop in a linked list
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;
typedef struct BusNetwork
{
int nBusStop; //number of bus stops in the newwork
struct AdjStopList **array;
} BusNetwork;
//create a new empty AdjBusStopList
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;
}
I tried to use a while loop to assign stopName with "test" in a function
BusNetwork *newBusNetwork(int n, const char *BusStops)
{
newBN->array = malloc(n * sizeof(AdjStopList*));
for (int i = 0; i < 10; i++)
{
newBN->array[i] = newAdjStopList();
strcpy(newBN->array[i]->stopName, "test");
}
//rest of the function
}
Then when I watch the variable (struct AdjStopList (**)[46])newBN->array in VScode, it seems only the first element of the array has been processed correctly, e.g. newBN->array[0]->stopName is test. The rest elements of the array still have random garbage values, e.g. newBN->array[1]->stopName.
Can anyone see why this is happening, please?