Quote:
Originally Posted by Arevos
JawaKing00 appears to understand the fact that a 2D array is simply an array of arrays, and he comprehends that a pointer can be assigned to an array. What he slips up on is to assume that arrays and pointers are the same thing.
The correct approach can be inferred by DaWei's excellent and knowledgable guide to pointers. However, as far as I can see, the correct way of pointing to a 2D array is ever explicitly mentioned, so I'll mention it now. This is partially for JawaKing00's benefit, partially for mine; if I'm incorrect, I'd like to know about it!
ITEM_DEFINE_t (*ItemPointer)[4]; ItemPointer = SecondItemDefinitionArray; printf ("%s", ItemPointer[1][1].NameString);
|
That appears to work perfectly. Thank you very much!
I still don't quite understand why, though. I read through both of those tutorials, and I do have a passing knowledge of pointers and arrays, but sometimes they just make my head hurt. I get that that is a pointer to an array of structures, but why is there only one dimension? Wouldn't
ITEM_DEFINE_t (*ItemPointer)[2][4];
seem to make more sense? It would to me, but that doesn't work.
Also, why do we need to use the '.' and not '->' when accessing a member of the structure in that array? I thought when using a pointer, that the '->' notation was used, but again that doesn't work.
Finally, why do we initialize the pointer as the whole array and not the address of the array? I would expect something like
ItemPointer = &SecondItemDefinitionArray[0][0];
and not
ItemPointer = SecondItemDefinitionArray;
I guess my inexperience with pointers to arrays is showing here, but I'd like to know more about the reasoning behind this. Especially since I'll probably need to explain it to the lead engineer on the project, since he doesn't appear to fully understand this implementation either
Thank you again for all the help. It has been incredibly useful.