I know that the following two things are the same in c (because of offsets and arrays)
someArray[i] //ith element of someArray
*(someArray + i) //ith element of someArray
However for structs, the same syntax doesn't seem to hold up very well...
someStruct[i]->*(someArray + j) //compiler error
*(someStruct + i)->someArray[j] //Also compiler error
Is there anyway to use the pointer/offset notation (the second one) to represent elements of a struct?
4 Answers 4
Assuming someStruct is an array of structs, and someArray is a struct member of array type, then either of these would be valid:
*(someStruct[i].someArray + j)
or
(*(someStruct + i)).someArray[j]
See e.g. http://ideone.com/UtLN2.
Comments
Among other things, you are using the pointer resolution operator -> when you should probably be using the member reference operator .
Assuming someStruct[] is an array of structs (not pointers):
*(someStruct[i].someArray + j)
(*(someStruct + i)).someArray[j]
Comments
From what I understood, I suppose that you want to represent fields of a structure through some pointer/offset arithmetic.
There's a standard macro, offsetof(type, field) in stddef.h which yields the offset of the field field inside the structure or union type type.
For example, suppose s is of type struct s*, and f is a field in struct s. Then *(s + offsetof(struct s, f)) is the same as s->f.
Comments
This should work
*(someStruct[i]->someArray + j)
(*(someStruct + i))->someArray[j]