![]() |
| | #1 |
| Registered User Join Date: Jul 2009 Location: AZERBAIJAN
Posts: 78
| 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);
}
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 | |
| | #2 | |
| Mysterious C++ User 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:
| |
| Elysia is offline | |
| | #3 |
| Registered User 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 | |
| | #4 | |
| Mysterious C++ User 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:
| |
| Elysia is offline | |
| | #5 |
| Registered User Join Date: Jul 2009 Location: AZERBAIJAN
Posts: 78
| ok thank u. |
| BlackOps is offline | |
| | #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 | |
| | #7 |
| Registered User 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 | |
| | #8 |
| DESTINY Join Date: Jul 2008 Location: in front of my computer
Posts: 803
| 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 | |
| | #9 |
| critical genius 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);}
Last edited by MK27; 07-21-2009 at 09:21 AM. |
| MK27 is offline | |
| | #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 | |
| | #11 | |
| DESTINY Join Date: Jul 2008 Location: in front of my computer
Posts: 803
| Quote:
__________________ 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 | |
| | #12 |
| Registered User Join Date: Jul 2009
Posts: 40
| |
| WatchTower is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
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 |