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?

1
  • 1
    Although I can tell what is happening, in general, please don't say "it's giving error" but tell us what that error is. Commented Jun 24, 2011 at 9:01

2 Answers 2

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.

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

5 Comments

@Cicada: Arrays are not pointers. Please stop spreading this dangerous misinformation.
Arrays are pointers! But have const nature, you can't move them
@Soltys: No, they completely are not. This is utter nonsense.
@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.
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.

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.