Thread: pointers to 2d arrays

  1. #1
    Registered User Diamonds's Avatar
    Join Date
    Oct 2002
    Posts
    68

    Post pointers to 2d arrays

    I have a class that works something like this:

    Code:
    class bleh {
    public:
          char *charArray;
          bleh(int size);
    private:
    };
    
    bleh::bleh(int size) {
         charArray = new char[size];
    }

    then I realized that I wanted charArray to be 2d
    however,
    Code:
    class bleh {
    public:
          char *charArray;
          bleh(int size1, size2);
    private:
    };
    
    bleh::bleh(int size1, int size2) {
         charArray = new char[size1][size2];
    }
    doesn't werk

    the only thing that I can get to work is somethint like:
    Code:
    class bleh {
    public:
          char charArray[100][100];
          bleh(int size);
    private:
    };
    
    bleh::bleh(int size) {
    }
    Keeps yelling about either (based on small changes on my part):
    A) doesn't like pointer
    B) can't cast
    C) doesn't like that a 2d array have variables for it's size

    Any clues how it can be done?

  2. #2
    Registered User
    Join Date
    Jun 2002
    Posts
    230
    Mabye this might help some.

    Code:
    class bleh {
    private:  // might want it private
          char charArray[100][100];
          bleh(int size);
    public: //Might want this public
    };
    
    bleh::bleh(int size) {
    }
    This might not help much but i changes private and public. Not sure if i can help you with the coding much. Ill try.
    C++ Rules!!!!
    ------------
    Microsoft Visual Studio .NET Enterprise

  3. #3
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Or you can try something like this:
    Code:
       char** array;
       const unsigned ROWS = 10;
       const unsigned COLS = 10;
    
       // allocate memory
       array = new char*[ROWS];
    
       for (i = 0; i < ROWS; ++i)
          array[i] = new char[COLS];
    ...
    
       // clean up memory
       for (i = 0; i < ROWS; ++i)
          delete array[i];
    
       delete array;

  4. #4
    Registered User Diamonds's Avatar
    Join Date
    Oct 2002
    Posts
    68

    thnx

    ah people are up this late ^_^ good

    Salem's answer is what I'm wanting, thnx

    I did a search and didn't find that post =/
    srry

  5. #5
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    You can use one dimentional array then deal with it as a two dimentional array.

    int *array;

    array = new int[columns*rows]

    so it's like putting the rows next to each other.

    You can access each element by the index array[x, y];
    like this;

    array[x, y]; is array[ columns * x + y ];
    where columns is the number of columns in the array.

    You can put it in a function it will make it easier for you.
    The function will return a pointer.

    Do you get it.
    Last edited by ammar; 10-31-2002 at 07:10 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointers to Multidimensional Arrays
    By kidburla in forum C Programming
    Replies: 10
    Last Post: 10-29-2005, 10:45 PM
  2. arrays of pointers to function?????
    By andyzjk in forum C++ Programming
    Replies: 11
    Last Post: 04-17-2005, 09:55 AM
  3. returning 2D arrays
    By ... in forum C++ Programming
    Replies: 2
    Last Post: 09-02-2003, 12:28 PM
  4. Initialising 2D and 3D arrays
    By fry in forum C++ Programming
    Replies: 5
    Last Post: 08-01-2002, 04:34 AM
  5. Hello all and pointers to multi-dimensional arrays
    By Mario in forum C++ Programming
    Replies: 16
    Last Post: 05-17-2002, 08:05 AM