4

I would like to get a two dimensional int array arr that I can access via arr[i][j].

As far as I understand I could declare int arr[10][15]; to get such an array. In my case the size is however variable and as far as I understand this syntax doesn't work if the size of the array isn't hardcoded but I use a variable like int arr[sizeX][sizeY].

What's the best workaround?

iammilind
  • 68,093
  • 33
  • 169
  • 336
Christian
  • 25,249
  • 40
  • 134
  • 225
  • 2
    Your question says C, but your tag says C++. Could you please clarify? Also, you're correct that using a variable to determine the size of the array isn't valid syntax unless `sizeX` and `sizeY` are `const`. However, gcc (or g++) support this unless you pass them the `-ansi` or `-pedantic` flags. I'd still recommend against using it as it's not standard C/C++. – Chris Parton Nov 28 '11 at 09:59

6 Answers6

11

If you don't want to use a std::vector of vectors (or the new C++11 std::array) then you have to allocate all sub-arrays manually:

int **arr = new int* [sizeX];
for (int i = 0; i < sizeX; i++)
    arr[i] = new int[sizeY];

And of course don't forget to delete[] all when done.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
1

c/c++ does not support multidimensional array. But it does support array of array:

    //simulate 2-dimension array with 1-dimension array
    {
        int x = 20;
        int y = 40;
        int * ar = new int(x*y);
        int idx_x =9;
        int idx_y=12;
        ar[idx_x + idx_y * x] = 23;
    }
    //simulate 2-dimension array with array of array
    {
        int x = 20;
        int y = 40;
        int**ar = new int*[y];
        for(int i = 0; i< y; ++i)
        {
            ar[i] = new int[x];
        }
        ar[9][12] = 0;      
    }
BruceAdi
  • 1,949
  • 1
  • 11
  • 8
0

As fefe said you can use vector of vectors, e. g. :

#include<iostream>
#include<vector>
using namespace std;

int main()
{
        vector< vector<int> > vec;
        vector<int> row1;
        row1.push_back(1);
        row1.push_back(2);
        vector<int> row2;
        row2.push_back(3);
        row2.push_back(4);

        vec.push_back(row1);
        vec.push_back(row2);

        for( int ix = 0; ix < 2; ++ix)
            for( int jx = 0; jx < 2; ++jx)
                cout << "[" << ix << ", " << jx << "] = " << vec[ix][jx] << endl;

}
cpp
  • 3,743
  • 3
  • 24
  • 38
0

If you're looking for the solution in C, see this Stack Overflow thread: How do I work with dynamic multi-dimensional arrays in C?.

If C++ is okay, then you can create a 2D vector, i.e. a vector of vectors. See http://www.cplusplus.com/forum/general/833/.

Community
  • 1
  • 1
Chris Parton
  • 1,052
  • 9
  • 16
0

You can't, with array syntax. The language requires that you use compile time constants to create arrays.

Daniel Daranas
  • 22,454
  • 9
  • 63
  • 116
  • You can use an `int**` (or whatever type you want). Once you've correctly `malloc()`ed it, you can access elements using two subscript operators. – Chris Parton Nov 28 '11 at 10:08
  • @Chris Parton: This is not array syntax. It's using pointers, and new, to emulate dynamically sized arrays. – Daniel Daranas Nov 28 '11 at 14:00
  • 1
    I never said it was :) I was just making the point that it's possible for him to access elements using pointers in the same fashion as you would a multidimensional array. To that end, the syntax is the same; but I agree that using pointers like that isn't array syntax. – Chris Parton Nov 29 '11 at 02:59
0

C++ does not have variable length arrays. You can consider using vector<vector<int> >. It can be also accessed using arr[i][j].

fefe
  • 3,342
  • 2
  • 23
  • 45