1

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?

asked Jun 11, 2012 at 19:58

4 Answers 4

5

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.

answered Jun 11, 2012 at 20:00
Sign up to request clarification or add additional context in comments.

Comments

1

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]
answered Jun 11, 2012 at 20:02

Comments

0

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.

answered Jun 11, 2012 at 20:10

Comments

0

This should work

*(someStruct[i]->someArray + j)

(*(someStruct + i))->someArray[j]

answered Jun 12, 2012 at 9:28

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.