1. ## Multidimensional Array Addressing

I was trying to find out a simple way to navigate through the multidimensional array, and understand it. And i decided to develop some simple technique which would let treat complicated arrays. As an example lets say i have the following array
ar[2][2][2][2] as you see it is a 6 dimensional array.

Well, two dimensional array is just a grid... 3 dimensional array could be imagined like a cube...but when there are more dimensions, then its more difficult to imagine it and navigate through it. Of course i can always create some pointer and navigate through it like this:
int *p = &ar[0][0][0][0];
and then i can just manipulate with a pointerm, *(p+offset).

But i can also imagine any multidimensional array as a tree... the first top element of tree is a name of array, then i do this: in case of ar[2][2][2][2], i see first number in first bracket from left is 2, so it has two elements, then i draw two elements under the first top element of tree, the next number in next brackets is 2, it means that each two elements have two elements...i draw them under each of them...and so on...

now i can access to the first element of the array with this ****ar, and i see that the number of '*' stars must equal the number of dimension the array has. And to access the next element i put ( after the first star from the left, then put +offset to the end, and then close with ), so... the 7th element is, *(***ar + 7).

I also see that now i can navigate through this tree by moving in all 4 directions through this tree, and now i can access to the element with index 7 in different ways... here is a code..

Code:
#include <stdio.h>

void main (int argc, char *argv[])
{

int ar[2][2][2][2] =  {{{{2,4},  {6,8}},  {{10,12}, {14,16}}},
{{{18,20}, {22,24}}, {{26,28}, {30,32}}}};

int x;

x = *(***ar+7);
x = *(**ar[0]+7);
x = *(*(*(ar[0]+2)-1)+1);
x = *(*(*(*ar+1)+1)+1);
x = *(***(ar+1)-1);

printf("%d",x);
}
So the point is to use a tree as a map for any type of multidimensional array... of course probably you wont need these to use in practice, but it just can give another quick help in imagination of such an array..

But one thing which i want to ask the experienced programmers is... can i always be sure that any of such kind of addressing will work? Because some man said to me, that it will work only if all the elements of this multidimensional array would lay down in memory continiously one after another... But aren't they contiguous in case of array?

Right now, in this program, all those expressions of x = give value of 16. the 7th element. So, can i always be sure that any of those expressions will work ALWAYS?

thanks

2. I want to ask you why you make it so complicated.
There is a reason in the first place why we have array notation.
If you have,
T x[n1][n2];
You can even do &x[0][n] to the the address of the nth column of first row.

And btw, do not use void main!
SourceForge.net: Void main - cpwiki

3. well i just said that i did it to get a better imagination of how to deal with multi dimensional array...not just 2 or 3 dimensional... and just to feel comfortable to access any elements in many possible and correct ways. that was my point. and i also said that it doesnt mean that one has to use it...just another way of doin this..

and my question was are many dimensional arrays lay down as contiguous blocks in memory?

4. Yes, they are. Which makes it so easy.

5. ok thank u.

6. > x = *(***ar+7);

the problem with this is that you're overrunning the last array, invoking undefined behavior.

See N1256 6.5.6p8

comp.lang.c | Google Groups

7. hm but there is no undefined behaviour it works fine... because i dont use another pointer to array, i am beginning to work with the array name itself..

8. Originally Posted by BlackOps
hm but there is no undefined behaviour it works fine...
Undefined behaviour itself means that it may work with some compiler and may not work with others.

9. The fact that you can do this does not mean that it is a good idea. Just use subscript notation, as Elysia suggests (eg, a[2][4][1], etc).

Doing it your way is basically a form of obfuscation* IMO. If you do things in non-standard, intentionally awkward ways, people will not want to work with you or your code...

* there's a great example on that wikipedia page:
Code:
#include <stdio.h>
main(t,_,a)char *a;{return!0<t?t<3?main(-79,-13,a+main(-87,1-_,
main(-86,0,a+1)+a)):1,t<_?main(t+1,_,a):3,main(-94,-27+t,a)&&t==2?_<13?
main(2,_+1,"%s %d %d\n"):9:16:t<0?t<-72?main(_,t,
"@n'+,#'/*{}w+/w#cdnr/+,{}r/*de}+,/*{*+,/w{%+,/w#q#n+,/#{l,+,/n{n+,/+#n+,/#\
;#q#n+,/+k#;*+,/'r :'d*'3,}{w+K w'K:'+}e#';dq#'l \
q#'+d'K#!/+k#;q#'r}eKK#}w'r}eKK{nl]'/#;#q#n'){)#}w'){){nl]'/+#n';d}rw' i;# \
){nl]!/n{n#'; r{#w'r nc{nl]'/#{l,+'K {rw' iK{;[{nl]'/w#q#n'wk nw' \
iwk{KK{nl]!/w{%'l##w#' i; :{nl]'/*{q#'ld;r'}{nlwb!/*de}'c \
;;{nl'-{}rw]'/+,}##'*}#nc,',#nw]'/+kd'+e}+;#'rdq#w! nr'/ ') }+}{rl#'{n' ')# \
}'+}##(!!/")
:t<-50?_==*a?putchar(31[a]):main(-65,_,a+1):main((*a=='/')+t,_,a+1)
:0<t?main(2,2,"%s"):*a=='/'||main(0,main(-61,*a,
"!ek;dc i@bK'(q)-[w]*%n+r3#l,{}:\nuwloca-O;m .vpbks,fxntdCeghiry"),a+1);}
credited to Ian Phillipps. This is a perfectly legal C program which compiles without error. Nice job! Wouldn't it be fun if we all wrote that way?

10. by "last array", I meant arr[a][b][c][d] <-- the d indexes the last array, and the "last array" is not only arr[1][1][1]. so arr[0][1][0] would be, in my ill-chosen words, a "last array".

basically, I meant the most minor arrays.

11. Originally Posted by MK27
The fact that you can do this does not mean that it is a good idea. Just use subscript notation, as Elysia suggests (eg, a[2][4][1], etc).

Doing it your way is basically a form of obfuscation* IMO. If you do things in non-standard, intentionally awkward ways, people will not want to work with you or your code...

* there's a great example on that wikipedia page:
Code:
#include <stdio.h>
main(t,_,a)char *a;{return!0<t?t<3?main(-79,-13,a+main(-87,1-_,
main(-86,0,a+1)+a)):1,t<_?main(t+1,_,a):3,main(-94,-27+t,a)&&t==2?_<13?
main(2,_+1,"%s %d %d\n"):9:16:t<0?t<-72?main(_,t,
"@n'+,#'/*{}w+/w#cdnr/+,{}r/*de}+,/*{*+,/w{%+,/w#q#n+,/#{l,+,/n{n+,/+#n+,/#\
;#q#n+,/+k#;*+,/'r :'d*'3,}{w+K w'K:'+}e#';dq#'l \
q#'+d'K#!/+k#;q#'r}eKK#}w'r}eKK{nl]'/#;#q#n'){)#}w'){){nl]'/+#n';d}rw' i;# \
){nl]!/n{n#'; r{#w'r nc{nl]'/#{l,+'K {rw' iK{;[{nl]'/w#q#n'wk nw' \
iwk{KK{nl]!/w{%'l##w#' i; :{nl]'/*{q#'ld;r'}{nlwb!/*de}'c \
;;{nl'-{}rw]'/+,}##'*}#nc,',#nw]'/+kd'+e}+;#'rdq#w! nr'/ ') }+}{rl#'{n' ')# \
}'+}##(!!/")
:t<-50?_==*a?putchar(31[a]):main(-65,_,a+1):main((*a=='/')+t,_,a+1)
:0<t?main(2,2,"%s"):*a=='/'||main(0,main(-61,*a,
"!ek;dc i@bK'(q)-[w]*%n+r3#l,{}:\nuwloca-O;m .vpbks,fxntdCeghiry"),a+1);}
credited to Ian Phillipps. This is a perfectly legal C program which compiles without error. Nice job! Wouldn't it be fun if we all wrote that way?
Looking at the code it looks as if it has more than 1 main() or are they call to main(). But seriously the output to this code was a surprise!!!!!!

12. Originally Posted by BEN10
Looking at the code it looks as if it has more than 1 main() or are they call to main(). But seriously the output to this code was a surprise!!!!!!
The output was truly a surprise, I never thought of this existing in C.. But a very good example though.

Popular pages Recent additions