Welcome, Guest. Please login or register.

Author Topic: Finding the address of an array element  (Read 8807 times)

Description:

0 Members and 1 Guest are viewing this topic.

Offline tonyw

  • Hero Member
  • *****
  • Join Date: Mar 2002
  • Posts: 553
    • Show all replies
Re: Finding the address of an array element
« on: March 14, 2003, 08:09:32 AM »
The question was "what is the general expression?"

For an n-dimensional array of elements 'type'

the declaration might be:
"type name[m1][m2][m3][ . . . ][mn];"

where "type" is int, char, etc;
"name" is the array name;
"m1", "m2" etc are the sizes of each subscript.

The address of an element (&name[M1][M2][. . . ][MN]) = (type *)name + MN * (sizeof(type) + M(N-1) * (sizeof(type) + . . . . +M1 * (sizeof(type))))))));  (N closing paren)

However, I could have it backwards, in which case, run the N -> 0 indices back the other way. Draw a sketch on a piece of paper, with the last subscript of the array changing fastest, and you should see it.


tony