Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Finding Array Addresses 1

Status
Not open for further replies.

DIsaacs

Programmer
Jul 27, 2002
6
US
if the address of a 2d array would be

Address of A[j] = start_address + ( ( j + (i*NumCols)) *sizeof(int) )

and the address of a 3d array would be

Address of A[j][k] = start_address + (k ( j + (i * NumRows)) * NumCols) * *sizeof(data_type)

what would be the address of a 4D array
A[j][k][l]

And a 5D array
A[j][k][l][m]

i just don't know how to get it ?
 
Code:
Address of A[i][j] = start_address + ( ( j + (i*NumCols)) *sizeof(int) )


Assuming A is 4x4
A[0][0] = 0
A[0][1] = 1
A[0][2] = 2
A[0][3] = 3
A[1][0] = 4
A[1][1] = 5
A[1][2] = 6
...
A[4][3] = 14
A[4][4] = 15


then in addressing it will look like:

ADDRESS: [start_address, +1, +2, +3, +4, +5, ..., +15 ]
VALUE  : [     0       ,  1,  2,  3,  4,  5, ...,  15 ]


Explanation:
You can group the address to something more comforting to you..


[ [ #, #, #, #], [ #, #, #, #], [ #, #, #, #], [ #, #, #, #] ]
--------------------------------------------------------------

[ [   i = 0   ], [   i = 1   ], [   i = 3   ], [   i = 4   ] ]

  (j=...)        (j=...)        (j=...)        (j=...)
[ [ 1, 2, 3, 4], [ 5, 6, 7, 8], [ 9,10,11,12], [13,14,15,16] ]




Address of A[i][j][k] = start_address + (k ( j + (i * NumRows)) * NumCols) * sizeof( int )

If A is a 2x2x2

A[0][0][0] = 0
A[0][0][1] = 1
A[0][1][0] = 2
A[0][1][1] = 3
A[1][0][0] = 4
A[1][0][1] = 5
A[1][1][0] = 6
A[1][1][1] = 7


ADDRESS: [start_address, +1, +2, +3, +4, +5, +6, +7 ]
VALUE  : [     0       ,  1,  2,  3,  4,  5,  6,  7 ]



[ [ # , # ], [ # , # ] ], [ [ # , # ], [ # , # ] ]
--------------------------------------------------

[        i = 0         ], [        i = 1         ]

[ [ j = 0 ], [ j = 1 ] ], [ [ j = 0 ], [ j = 1 ] ]

  (k=...)    (k=...)      (k=...)      (k=...)
[ [ 0 , 1 ], [ 0 , 1 ] ], [ [ 0 , 1 ], [ 0 , 1 ] ]



See a pattern going?



 --- now with this basic information: You might be able to figure out what you're asking...
 
I need a formula, i still don't get it
sorry
 
Mmm :/

I think your second formula is off...
start_address + (k ( j + (i * NumRows)) * NumCols) * sizeof( int )


it's actually supposed to be...

start_address + (((i * numRows * numCols ) + ( j * numCols) + k) * sizeof( int ))


 
For 4:
start_address + ( ( ( i * numRows * numCols * numZs ) + ( j * numRows * numCols ) + ( k * numRows ) + l ) * sizeof( int ) )
 
for 5:
start_address + ( ( ( i * numRows * numCols * numZs * numZ2s ) + ( j * numRows * numCols * numZs ) + ( k * numRows * numCols) + ( l * numRows ) + m ) * sizeof( int ) )



Hope this helped. Hope you get it too :/
 
ACK! Screwed up a bit. Sorry :X All these posts are me.. Haha.. - the last + numRows for 4 & 5? It's supposed to be + numCols... Sorry..
 
where are the + numRows ?
yeah i'm getting it
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top