0

So I have a pointer to a 2D array like so:

int board[3][5] = { 3, 5, 2, 2, 1, 3, 4, 34, 2, 2, 3, 4, 3, 223, 923 };
int* ptr[sizeof(board[0]) / sizeof(board[0][0])] = board;

I'm trying to follow this example. But for some reason I'm getting the error:

IntelliSense: initialization with '{...}' expected for aggregate object

Any idea what the problem is?

1
  • 1
    This question has an answer here Commented Jan 31, 2014 at 9:24

3 Answers 3

1

Assign pointer to the first element of the array like below

int (*ptr)[5] = board;

Note: Column size [5] in the pointer declaration should be equal to the original 2 dimension array column size [5]. Declaring row size [3] is optional.

int main() {

    int board[3][5] = { 3, 5, 2, 2, 1, 3, 4, 34, 2, 2, 3, 4, 3, 223, 923 };

    /*
    // 3 Rows 5 Columns Matrix
       int board[3][5] = { {3, 5, 2, 2, 1 },
                           {3, 4, 34, 2, 2 },
                           {3, 4, 3, 223, 923}
                         };
   */

   // Assign pointer to the first element of the array
   int (*ptr)[5] = board;

   for(int i=0; i< (3*5); i++) {
       std::cout<<(*ptr)[i]<<std::endl;
    }

   return 0;
}
Sign up to request clarification or add additional context in comments.

Comments

0

A 2D array is not the same as an array of pointers. You cannot directly convert one to the other.

Comments

0

I just needed to put () around the *ptr. I have no idea how this fixes it but now I can do ptr[1][2].

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.