Welcome, Guest. Please login or register.

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

Description:

0 Members and 1 Guest are viewing this topic.

Offline Blitter

  • Sr. Member
  • ****
  • Join Date: Feb 2002
  • Posts: 320
    • Show all replies
Re: Finding the address of an array element
« on: March 14, 2003, 01:47:57 AM »
This might get you pointed in the right direction.

Blitter
\\"What the hell am I looking at? When does this happen in the movie?\\"
- Dark Helmet
 

Offline Blitter

  • Sr. Member
  • ****
  • Join Date: Feb 2002
  • Posts: 320
    • Show all replies
Re: Finding the address of an array element
« Reply #1 on: March 14, 2003, 03:57:16 AM »
Okay, It's been a while since I've done this in C but I'll try and explain my understanding of it.

In actuality an array is a pointer to a set pf pointers, so a multi-dimentional array is a pointer to a pointer to set of pointers, etc.

I'll use a 3 dimentional array as an example.

ary
  • [y][z]


so we have x number of planes that have y number of rows that has z number of colums.

Now in order to traverse every element in this multi-dimentional array we have to go through every colum of every row of every plane. But we dont't want to do that. We want the pointer of a specific element in our multi-dimetional array.

So we know what we want but how to get it?  Well, since we have x planes that contain our y rows and z colums, then we can add them like this:

I'll use your example but with 3 dimentions.

int base
  • [y][z];

address = base[index1][index2][index3]

address = base+(index1* x * sizeof(int))+(index2 * y * sizeof(int))+(index3 * sizeof(int))

Okay, I was going to go into n-dimentional arrays but It's getting late and I'm starting to confuse myself now. :-P

Basically for an n-dimentional array you're gonna have to create a loop to traverse your array.

If this is of no help, then I appologize.  It's been quite a while since I've had to do this.

This contains more algorithyms for finding addresses.

Blitter
\\"What the hell am I looking at? When does this happen in the movie?\\"
- Dark Helmet
 

Offline Blitter

  • Sr. Member
  • ****
  • Join Date: Feb 2002
  • Posts: 320
    • Show all replies
Re: Finding the address of an array element
« Reply #2 on: March 14, 2003, 04:09:19 AM »
You know, it just hit me.  You stated contigious memory block.  I'm not sure what I posted would work in that situation and I'd have to go back to my books to find an ABSOLUTE solution.  If one of the more regular C programmers here can't help, then I'll did my books out this weekend and see what I can figure out for ya.

Also if you have time, this is an interresting read.

It's for TI-89 and TI-92+ calculators, but also covers some of the pointer inderection stuff that I wanted to get into for ya.

Blitter
\\"What the hell am I looking at? When does this happen in the movie?\\"
- Dark Helmet