What's the difference...

Posted by Ithildin on Thu 02 Dec 2004 05:15 AM — 3 posts, 14,474 views.

USA #0
What's the difference between:

skill_table[sn]->spell_level

and

skill_table[sn].spell_level
USA #1
Well, only one of them is valid, depending on what type of thing you're storing.

First things first...

skill_table[sn]
This is equivalent to:
*(skill_table+sn)
In other words, take the address skill_table, move 'sn' elements forward, and get that value. This is what array indexing really is.

Let us call the result of this operation "s", for simplicity.

Now, for:
s.bla
and
s->bla

s.bla means that s is in fact a structure, and so we can directly access the 'bla' member.
s->bla means that s is a pointer to a structure, so we must first dereference (follow) the pointer, and then do the normal structure retrieval to get the 'bla' member.

If skill_table is an array of pointers to structs, you must use s->bla. If skill_table is an array of structs, you must use s.bla.

Array of pointers to structs:
struct foo { ... };
foo* * myArray;

Array of structs:
foo * myArray;

Note that an array is in fact just a pointer that happens to point to a contiguous block. But, for all intents and purposes, a pointer to an array is the same thing as a pointer to a single element. (This is why strings in C are arrays and also char*, remember.)

Does this answer your question?
USA #2
Yes, that helps out heaps.

Thanks for the help.