Assignment of Two-Dimensional Arrays

This is a discussion on Assignment of Two-Dimensional Arrays within the C++ Programming forums, part of the General Programming Boards category; Okay, this may seem a bit of an elementary question. If so, please forgive me. I've initialized a two-dimensional array ...

  1. #1
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856

    Assignment of Two-Dimensional Arrays

    Okay, this may seem a bit of an elementary question. If so, please forgive me.

    I've initialized a two-dimensional array like so, for example:
    Code:
    static const int 2DARRAY[2][3] = {
      { 1, 2, 3 },
      { 4, 5, 6 }
    };
    I have multiple arrays declared that way and depending on what level we want to load I need to generically access one of the 2D arrays and that's where I keep causing a page fault.

    Before realizing my mistake I was trying something similar to this: "const int ***p2dArr = (const int***)2DARRAY;"

    When I do a (*p2dArr)[0][0] I crash because the size of the array is unknown. How can I work around this limitation?

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    you could place the 2D array in a struct/class and then have an container holding instances of the struct/class or you could create a 3D array, static or dynamic, as you need.


    int threeDArray[4][2][3];

    threeDArray[0] is the first of 4 2D arrays, each 2 by 3, that could be in threeDarray, etc.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,333
    The notation for the pointer is exactly the same as if you were passing these arrays to a function
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main ( ) {
        int one[2][3] = { { 1, 2, 3 }, { 4, 5, 6 } };
        int two[2][3] = { { 7, 6, 5 }, { 4, 3, 2 } };
        int (*p)[3];
        int r,c;
        p = one;
        for ( r = 0 ; r < 2 ; r++ ) {
            for ( c = 0 ; c < 3 ; c++ ) {
                printf( "%d ", p[r][c] );
            }
            printf( "\n" );
        }
        return 0;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User manofsteel972's Avatar
    Join Date
    Mar 2004
    Posts
    317
    Code:
    static const int T2DARRAY[2][3] = {
      { 1,2,3 },
      { 4,5,6 }
    };
    
    const int (*p2dARR)[3]=T2DARRAY;
    
    cout<< p2dARR[0][0];
    This works on Visual C++ compiler I had to change the name of the array because it starts with a number
    Last edited by manofsteel972; 08-06-2004 at 03:29 PM.
    "Knowledge is proud that she knows so much; Wisdom is humble that she knows no more."
    -- Cowper

    Operating Systems=Slackware Linux 9.1,Windows 98/Xp
    Compilers=gcc 3.2.3, Visual C++ 6.0, DevC++(Mingw)

    You may teach a person from now until doom's day, but that person will only know what he learns himself.

    Now I know what doesn't work.

    A problem is understood by solving it, not by pondering it.

    For a bit of humor check out xkcd web comic http://xkcd.com/235/

  5. #5
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    It was surprisingly as simple as that... Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Two dimensional arrays
    By Masschino in forum C Programming
    Replies: 9
    Last Post: 05-18-2004, 08:17 PM
  2. Replies: 6
    Last Post: 04-26-2004, 10:02 PM
  3. n dimensional arrays
    By linuxdude in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 04-20-2004, 02:10 AM
  4. Have you ever had a four dimensional experience
    By Silvercord in forum A Brief History of Cprogramming.com
    Replies: 35
    Last Post: 08-18-2003, 06:13 PM
  5. Passing 2 dimensional Arrays to functions
    By aresashura in forum C++ Programming
    Replies: 4
    Last Post: 12-17-2001, 11:59 PM

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