Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm trying to get to the bottom of an error, I wonder if you could help please? I'm having a bit of an issue with array pointers at the moment. I have three classes, one parent, and two children so to speak. One of the children has a 2D array of type struct, and I'm trying to access the elements of this from the other child.

I was wondering, is this code valid with the correct format/syntax for my array pointers? OChild1 creates and fills out the 2D array, I'm saving a pointer to that in Parent, and passing that pointer to OChild2, and then plan on using the contents of the array for further processing.

struct BOARDTILE
{
    float fPosX;
    float fPosY;

    BOARDTILES()
    {
        fPosX = 0.0f;
        fPosY = 0.0f;
    }
};

class CChild1
{
    public:
        BOARDTILE BoardTileArray[18][18];

    CChild1()
    {
    }

    writeBoardTileArray()
    {
        for (int i = 0; i <= 17; i++)
        {
            for (int j = 0; j <= 17; j++)
            {
                BoardTileArray[i][j].fPosX = (float) i * 5.0f;
                BoardTileArray[i][j].fPosY = (float) j * 7.0f;
            }
        }
    }
};

class CChild2
{
    public:
        BOARDTILE (*pBoardTileArray)[18][18];
        float fPosX;
        float fPosY;

    CChild2()
    {
    }

    void readBoardTileArray()
    {
        for (int i = 0; i <= 17; i++)
        {
            for (int j = 0; j <= 17; j++)
            {
                fPosX = (*pBoardTileArray[i][j]).fPosX;
                fPosY = (*pBoardTileArray[i][j]).fPosY;
                cout << fPosX;
                cout << fPosY;
            }
        }
    }

};

class CParent
{
    public:
        BOARDTILE (*pBoardTileArray)[18][18];
        CChild1 OChild1;
        CChild2 OChild2;

    CParent()
    {
        OChild1.writeBoardTileArray();
        pBoardTileArray = &(OChild1.BoardTileArray);
        OChild2.pBoardTileArray = pBoardTileArray;
    }
};
share|improve this question

1 Answer 1

up vote 1 down vote accepted

To store address of a two dimensional array, you would need a pointer to pointer,

Without typedef:
You would declare a member pointer as:

BOARDTILE (*pBoardTileArray)[18];

and then in the constructor, you would write:

    pBoardTileArray = OChild1.BoardTileArray;

But as you can see it makes the code complex. Using typedef can simplify it:

With typedef: (Recommended way)
With typedef you can write the following in the global scope:

typedef BOARDTILE BOARDTILE_ar18_t[18];

and then you can declare member pointer as:

BOARDTILE_ar18_t* pBoardTileArray;
share|improve this answer
    
Thanks for the reply. So, would I need to write the line above "BOARDTILE ( * pBoardTileArray)[18][18];" as BOARDTILE ( * * pBoardTileArray)[18][18]; instead? Or perhaps "BOARDTILE* ( * pBoardTileArray)[18][18];"? If I declare it as BOARDTILE** pBoardTileArray; I seem to get a conversion error with the line: pBoardTileArray = &(OChild1.BoardTileArray); Would you have a link to a page where I could learn more about this please? –  Moph Apr 18 '13 at 3:59
    
Or what about... BOARDTILE (*(*pBoardTileArray)[18])[18]; ? –  Moph Apr 18 '13 at 5:12
    
See my updated answer. –  Ali Apr 18 '13 at 6:28
    
Thank you very much, I really appreciate your help. My only problem now is working out how to read the contents of the 2D array once the pointer to it has been passed to the other object. I'm using the version without the typedef at the moment if that makes any difference? I think I'm slowly beginning to understand the concept of this, but it's definitely a slippery topic. I think the hard part to grasp is that the array is a set of pointers to begin with. –  Moph Apr 19 '13 at 1:06
    
I'm attempting to access the array using the lines: fPosX = (*pBoardTileArray[i][j]).fPosX; fPosX = (*pBoardTileArray[i])[j].fPosX; fPosX = (*pBoardTileArray)[i][j].fPosX; or fPosX = *pBoardTileArray[i][j].fPosX; But none of these seem to work. –  Moph Apr 19 '13 at 1:14

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.