Consider:
Code:
char *ptr = "abcde";
char c;
c = ptr[ 0 ];
This is fine. Indexing further down the line is also fine, so long as you actually have something valid to point to. You were doing this:
Code:
struct foo *ptr = malloc( sizeof *ptr );
...
ptr[ 1 ].something = somethingelse;
You can't index 1, because you've only allocated enough space for one structure. Thus, the only valid index above would be:
Code:
ptr[ 0 ].something = somethingelse;
Or, if you prefer:
Code:
ptr->something = somethingelse;
You were trying to walk off the end of your allocated space. That's why it wasn't working.
Yes, any single pointer can be done like so:
Code:
int *ptr;
int x;
ptr = &x;
ptr[0] = 10;
It's fine to use array style indexing, so long as you're actually accessing valid memory. You can't just do that if you don't actually have something valid at that location.
Quzah.