Pointers to multi dimensional arrays.

This is a discussion on Pointers to multi dimensional arrays. within the C++ Programming forums, part of the General Programming Boards category; Hi I was creating a class to initalize a series of blocks like in a break out game. I created ...

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    76

    Pointers to multi dimensional arrays.

    Hi I was creating a class to initalize a series of blocks like in a break out game. I created a pointer as a private member and then used this code to initalize it.

    Code:
    blockPointer = new Block[3][6];
    Then Visual C++ started generating errors like. Can't convert Block * to Block * [3]....

    I thought you could create pointer to arrays like that. Is there something I am neglecting??

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Is there something I am neglecting??
    blockPointer = new Block[3][6];


    Yeah, the type on the left has to match the type on the right. The leftmost dimension isn't part of the type of an array, so the type on the right is:

    Block[][6]

    Since new returns an address of dynamically allocated memory for that type, you have to make sure your variable blockPointer was declared as a pointer to that type. The syntax for that is:

    Block (*blockPointer)[6];

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    76
    Thanks mate I will remember that I just decided in the end it would be easier instead of creating a multi array of [3][6]. To jsut create a single one of [18]. Otherwise I would have to edit all my class files and considering it was a simple example I didnt want to have to do that.
    Thanks agen

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Structures, arrays, pointers, malloc.
    By omnificient in forum C Programming
    Replies: 9
    Last Post: 02-29-2008, 12:05 PM
  2. vector of arrays of pointers to structures
    By Marksman in forum C++ Programming
    Replies: 13
    Last Post: 02-01-2008, 04:44 AM
  3. pointers & arrays and realloc!
    By zesty in forum C Programming
    Replies: 14
    Last Post: 01-19-2008, 04:24 PM
  4. pointers to arrays of structures
    By terryrmcgowan in forum C Programming
    Replies: 1
    Last Post: 06-25-2003, 10:04 AM
  5. Help understanding arrays and pointers
    By James00 in forum C Programming
    Replies: 2
    Last Post: 05-27-2003, 02:41 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21