Thread: Multidimensional Array Addressing

  1. #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

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #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?

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yes, they are. Which makes it so easy.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User BlackOps's Avatar
    Join Date
    Jul 2009
    Location
    AZERBAIJAN
    Posts
    78
    ok thank u.

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    > 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. #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..

  8. #8
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    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

  9. #9
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    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?
    Last edited by MK27; 07-21-2009 at 09:21 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  10. #10
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    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.

  11. #11
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    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

  12. #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.

Popular pages Recent additions subscribe to a feed

Similar Threads

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