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?

1
  • 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++. Commented Nov 28, 2011 at 9:59

6 Answers 6

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.

Sign up to request clarification or add additional context in comments.

1 Comment

@Some Programmer dude how to delete that? is this way true? delete []arr[] ?
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;      
    }

Comments

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/.

Comments

0

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

3 Comments

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: This is not array syntax. It's using pointers, and new, to emulate dynamically sized arrays.
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.
0

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

Comments

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;

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.