Drowning in trying to understand how to dereference pointers in a linked list here...
Let's say I have a simple struct, called entry, which has char * name and int ID entry* next fields.
If I know I'll have 10 of them, it seems like the way to go is to make an array of pointers to structs:
entry* ptrArray[10];
and then loop i = 0 -> 10
ptrArray[i] = malloc(sizeof(entry));
I am running into a bunch of issues dealing with getting things in and out of here.
To input characters I only need to dereference the pointer in the array using -> correct? eg
scanf("%s", ptrArray[i]->name);
Because the dereference here points to the block of memory of the start of the char * correct?
But if I want to put in a number, I need to use the & operator... eg
scanf("%d", &ptrArray[i]->ID);
Is this because the int doesn't point to the address so I need to fetch it?
Then if I want to link the list together in this array, could I loop through 0 -10 and for 1 through 9 do something like:
ptrArray[i]->next = ptrArray[i+1];
It seems like I should be able to because the array just has pointers to structs and I'm just adding one.
Now let's say I want to print these guys. Is it as simple as
printf("%s 's ID is %d", ptrArray[i]->name, ptrArray[i]->ID);
I'm having a ton of trouble understanding how the array number the -> notation and the * to dereference works in this situation. I thought I understood it but I apparently do not, since when I run code like this I get segfaults.
Thanks for any help.