Multi-Dimension Array Declaration...

This is a discussion on Multi-Dimension Array Declaration... within the C++ Programming forums, part of the General Programming Boards category; Hi, Im currently teaching myself c++ and translating some of my java programs, to ease the transition. Could anyone tell ...

  1. #1
    HIM
    HIM is offline
    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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    you cannot declare static arrays without the size
    use some class suitable for your needs that provides an dynamic array support
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    712
    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
    HIM
    HIM is offline
    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, 01: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, 07:39 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 08:48 PM
  5. multi array to function
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 09-09-2001, 04:01 AM

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