0

How to initialize a 2D array with pointer.

int *mapTable[] = { {1,10} , {2,20} , {3,30} , {4,40} };  // It's giving error

Here int *mapTable is giving an error.

How can I declare them properly?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Sijith
  • 3,740
  • 17
  • 61
  • 101

2 Answers2

7

int *mapTable[] is not a 2D array: it is a 1D array of pointers.

But then you go and use a 2D array initialiser { {1,10} , {2,20} , {3,30} , {4,40} }.

That's why it's "giving error".


The 2D array way

Try:

int mapTable[][2] = { {1,10} , {2,20} , {3,30} , {4,40} };

And, yes, you do need to specify the size of that second dimension.


The 1D array of pointers way

This is a little more involved, and is usually too complex to be worth it.

It also usually requires dynamic allocation, causing a total mess with object lifetime:

int *mapTable[] = { new int[2], new int[2], new int[2], new int[2] };

int main() {
   mapTable[0][0] = 1; mapTable[0][1] = 10;
   mapTable[1][0] = 2; mapTable[1][1] = 20;
   mapTable[2][0] = 3; mapTable[2][1] = 30;
   mapTable[3][0] = 4; mapTable[3][1] = 40;

   // then, at the end of your program:
   for (size_t i = 0; i < 4; i++)
      delete[] mapTable[i];
}

As you can see, this is not ideal.

You can avoid dynamic allocation:

int mapTable0[] = {1,10};
int mapTable1[] = {2,20};
int mapTable2[] = {3,30};
int mapTable3[] = {4,40};
int *mapTable[] = { &mapTable0[0], &mapTable1[0], &mapTable2[0], &mapTable3[0] };

But I'm not sure why you'd want to go down this avenue.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • 4
    @Cicada: **Arrays are _not_ pointers**. Please stop spreading this dangerous misinformation. – Lightness Races in Orbit Jun 24 '11 at 09:07
  • **Arrays are pointers!** But have const nature, you can't move them – Paweł Sołtysiak Jun 24 '11 at 09:16
  • 4
    @Soltys: No, [they completely are not](http://stackoverflow.com/questions/1641957/is-array-name-a-pointer-in-c). This is utter nonsense. – Lightness Races in Orbit Jun 24 '11 at 09:17
  • 4
    @Soltys: no, they're not. The name of an array, under the right circumstances, can decay to an rvalue that's of the right type to be assigned to a pointer -- but even then, the array definitely is *not* a pointer. Calling it a pointer is like calling `1` an `int` variable. It's a value that can be assigned to an `int` variable, but it's not an `int` variable itself at all. – Jerry Coffin Jun 24 '11 at 09:20
  • 2
    @Soltys: [How do I use arrays in C++?](http://stackoverflow.com/questions/4810664/how-do-i-use-arrays-in-c). – sbi Jun 24 '11 at 09:42
0

The questin is tagged C++ and C; but all these answers only cover C++ (for the pointer way).

So in C, you can declare the rows separately and then compose them.

int r0[] = {1,10}, r1[] = {2,20}, r2[] = {3,30}, r3[] = {4,40};
int *mapTable[] = { r0, r1, r2, r3 };

Or, using C99, you can make anonymous arrays in the initializer.

int *mapTable[] = { (int[]){1,10}, (int[]){2,20}, (int[]){3,30}, (int[]){4,40} };

Both of these use the fact that an array reference decays into a pointer.

luser droog
  • 18,988
  • 3
  • 53
  • 105