So I'm using an array to store sructs (struct is Path). Defined as such:
Path **array = malloc(items * sizeof(Path *));
Then each element is alloc'd:
for (i=0;i<items;i++)
{
array[i] = malloc(sizeof(Path *));
}
Later in the program variables are updated as such:
Path *path = array[id];
path->next_hop=-1;
path->weight=INT_MAX;
I then have a separate function to print the values of the array, taking the pointer to the first item in the array (array[0]) and the size of array as parameters, declared as such:
void PrintTable(Path *paths, int n)
{
if(paths == NULL || n <= 0)
return;
printf("%s %8s %8s\n", "Net ID", "Weight", "Next Hop");
for(int i = 0; i < n; i++)
{
Path *p = paths + i;
printf("%d %8.2f %8d\n", vertices[i],p->weight, p->next_hop);
}
}
When I was debugging I noticed that the stuct pointers seem to be in memory addresses with locations 0x000000000020 apart while in the loop the memory addresses being read are 0x000000000010 apart. Therefore by making this change:
Path *p = paths + i;
to
Path *p = paths + (2 * i);
It seems to work. Why is this? And how do I fix it?
Path *pathsbePath *paths[]?