C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 07-20-2009, 12:58 PM   #1
Registered User
 
BlackOps's Avatar
 
Join Date: Jul 2009
Location: AZERBAIJAN
Posts: 78
Exclamation 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
BlackOps is offline   Reply With Quote
Old 07-20-2009, 01:17 PM   #2
Mysterious C++ User
 
Elysia's Avatar
 
Join Date: Oct 2007
Posts: 14,783
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
__________________
Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System
I dedicated my life to helping others. This is only a small sample of what they said:
"Thanks Elysia. You're a programming master! How the hell do you know every thing?"
Quoted... at least once.
Quote:
Originally Posted by cpjust
If C++ is 2 steps forward from C, then I'd say Java is 1 step forward and 2 steps back.
Elysia is offline   Reply With Quote
Old 07-20-2009, 03:00 PM   #3
Registered User
 
BlackOps's Avatar
 
Join Date: Jul 2009
Location: AZERBAIJAN
Posts: 78
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?
BlackOps is offline   Reply With Quote
Old 07-20-2009, 03:07 PM   #4
Mysterious C++ User
 
Elysia's Avatar
 
Join Date: Oct 2007
Posts: 14,783
Yes, they are. Which makes it so easy.
__________________
Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System
I dedicated my life to helping others. This is only a small sample of what they said:
"Thanks Elysia. You're a programming master! How the hell do you know every thing?"
Quoted... at least once.
Quote:
Originally Posted by cpjust
If C++ is 2 steps forward from C, then I'd say Java is 1 step forward and 2 steps back.
Elysia is offline   Reply With Quote
Old 07-20-2009, 03:10 PM   #5
Registered User
 
BlackOps's Avatar
 
Join Date: Jul 2009
Location: AZERBAIJAN
Posts: 78
ok thank u.
BlackOps is offline   Reply With Quote
Old 07-20-2009, 07:43 PM   #6
Registered User
 
Join Date: Oct 2001
Posts: 2,110
> 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
robwhit is offline   Reply With Quote
Old 07-21-2009, 08:33 AM   #7
Registered User
 
BlackOps's Avatar
 
Join Date: Jul 2009
Location: AZERBAIJAN
Posts: 78
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..
BlackOps is offline   Reply With Quote
Old 07-21-2009, 09:04 AM   #8
DESTINY
 
BEN10's Avatar
 
Join Date: Jul 2008
Location: in front of my computer
Posts: 803
Quote:
Originally Posted by BlackOps View Post
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.
__________________
HOPE YOU UNDERSTAND.......

By associating with wise people you will become wise yourself
It's fine to celebrate success but it is more important to heed the lessons of failure
We've got to put a lot of money into changing behavior


PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
IDE- Microsoft Visual Studio 2008 Express Edition
BEN10 is offline   Reply With Quote
Old 07-21-2009, 09:15 AM   #9
critical genius
 
MK27's Avatar
 
Join Date: Jul 2008
Location: SE Queens
Posts: 5,198
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?
__________________

"A man can't just sit around." -- Larry Walters

Last edited by MK27; 07-21-2009 at 09:21 AM.
MK27 is offline   Reply With Quote
Old 07-21-2009, 10:46 AM   #10
Registered User
 
Join Date: Oct 2001
Posts: 2,110
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.

Last edited by robwhit; 07-21-2009 at 10:54 AM.
robwhit is offline   Reply With Quote
Old 07-21-2009, 09:18 PM   #11
DESTINY
 
BEN10's Avatar
 
Join Date: Jul 2008
Location: in front of my computer
Posts: 803
Quote:
Originally Posted by MK27 View Post
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!!!!!!
__________________
HOPE YOU UNDERSTAND.......

By associating with wise people you will become wise yourself
It's fine to celebrate success but it is more important to heed the lessons of failure
We've got to put a lot of money into changing behavior


PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
IDE- Microsoft Visual Studio 2008 Express Edition
BEN10 is offline   Reply With Quote
Old 07-21-2009, 09:26 PM   #12
Registered User
 
Join Date: Jul 2009
Posts: 40
Quote:
Originally Posted by BEN10 View Post
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.
WatchTower is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Help -- allocating memory in a multidimensional array jonathan.plumb C Programming 10 05-20-2009 11:04 PM
Pointer to multidimensional array ejohns85 C++ Programming 4 03-24-2009 11:17 AM
Creating a Spreadsheet using a multidimensional array Apiatan C++ Programming 1 03-21-2009 04:18 PM
Type and nontype parameters w/overloading Mr_LJ C++ Programming 3 01-02-2004 01:01 AM
Help with an Array omalleys C Programming 1 07-01-2002 08:31 AM


All times are GMT -6. The time now is 02:04 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22