Thread: Multi-Dimension Array Declaration...

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    13

    Multi-Dimension Array Declaration...

    Hi, Im currently teaching myself c++ and translating some of my java programs, to ease the transition. Could anyone tell me how to translate the following piece of code

    Code:
     
    
    int[][] myArray;
    
    public void doSomething(int[][] myArray)
    {
          this.myarray = myarray;
    }
    I've tried what i thought would be logical, in the header file having

    Code:
    int myArray[][];
    then in the class file

    Code:
    void ThisClass::doSomething(int array[][])
    {
         myArray = array;
    }
    But it wants the initial dimensions size when I try to compile it. Problem is, I wont know how big the array is, untill the method receives it.
    Last edited by HIM; 10-31-2006 at 04:06 AM.

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    you cannot declare static arrays without the size
    use some class suitable for your needs that provides an dynamic array support
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    715
    Yes, as vart suggested, you need to pack it in some kind of class.
    Other solution woud be to use vector of vectors of type int.
    Look at http://www.cppreference.com/cppvector/index.html.
    If you're interested i can give you a small code snippet...
    Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
    It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem

  4. #4
    Registered User
    Join Date
    Aug 2006
    Posts
    13
    Thanks for the advice guys. I decided to use a pointer to the address of the first element of the array, then make my own method to translate the standard myArray[i][j] reference, into a single increment for the pointer. This way means it'll be easier for my team mate on the project to deal with the array on his end...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 04-25-2006, 12:14 AM
  2. Errors with including winsock 2 lib
    By gamingdl'er in forum C++ Programming
    Replies: 3
    Last Post: 12-05-2005, 08:13 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. multi array to function
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 09-09-2001, 03:01 AM