Welcome, Guest. Please login or register.

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

Description:

0 Members and 1 Guest are viewing this topic.

Offline Agafaster

  • Hero Member
  • *****
  • Join Date: Feb 2002
  • Posts: 1175
    • Show all replies
Re: Finding the address of an array element
« on: March 14, 2003, 03:58:03 PM »
Personally, I wouldnt bother - you'll only confuse yourself !

stick to 1 or 2 dimensional arrays where the index maths is much easier - if you need more dimensions, have several 1 or 2 d arrays of the same size:

ie:
int x[1000], y[1000], z[1000];

for 3d objects, etc you could use this:

typedef coord struct {int x, y, z };

coord array[1000];


in effect an array of structures.
the coords can then be accessed like this :

px = array[123].x;

etc. or

int curr_ind = 123;
ptr_curr_x = array + curr_ind * sizeof(coord);
ptr_curr_y = array + curr_ind * sizeof(coord) + sizeof(int);
ptr_curr_z = array + curr_ind * sizeof(coord) + 2 * sizeof(int);


This last approach can quite neatly be expanded for any number of dimensions.
why do you need lots of dimensions ? complicated Tensor Analysis ?  :-D
\\"New Bruce here will be teaching Machiavelli, Bentham, Locke, Hobbes, Sutcliffe, Bradman, Lindwall, Miller, Hassett and Benaud.\\"
\\"Those are all cricketers, Bruce !\\"
A1XE G3/800MHz Radeon 7000 512MB
A1200 030/25MHz 8MB