Functions with pointers to arrays of arrays

I've been trying to use a 10x10 matrix:

int nAreaGrid[10][10];

to represent a two dimensional area of 100 square units. To do this I've been intending for each square to have a number that represents what is in that square. I thought that the best way to map out a number to each square would be a function like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
void GridWriter(int nOccupier, int nGrid[10][10])
    {
        int* pnGrid = nGrid;

        for(int y = 0; y < 10; y++, *pnGrid++)
        {
            for(int x = 0; x < 10; x++, *pnGrid++)
            {
                if(y < 5 && x < 5)
                {
                    *pnGrid = nOccupier;
                }
                else
                {
                    *pnGrid = 0;
                }

            }
        }
    }


The idea is that it puts a square of occupied space in the top left corner of the grid and leaves the rest unoccupied, but I get the error message:

error: cannot convert 'int (*) [10]' to 'int*' in initialization.


I'm pretty sure that this is a casting error, but I've played around for a while and I'm not sure how to fix it. So, is there a way to point to a matrix?
Inside your function you can use subscriptors for your array the usual way.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void GridWriter( int nGrid[10][10], int nOccupier  )
{

        for ( int y = 0; y < 10; y++ )
        {
            for ( int x = 0; x < 10; x++ )
            {
                if ( y < 5 && x < 5 )
                {
                    pnGrid[y][x] = nOccupier;
                }
                else
                {
                    pnGrid[y][x] = 0;
                }

            }
        }
    }


As for the error

error: cannot convert 'int (*) [10]' to 'int*' in initialization.


The function parameter has type int (*) [10] while you are trying to assign it to pnGrid that is defined as int *
int* pnGrid = nGrid;

You shall write

int ( *pnGrid )[10] = nGrid;
Last edited on
If I write the function the way you have it, is int (*pnGrid)[10] = nGrid; supposed to go in the parameter of the function definition?
I have not understood what you said. I showed already the function definition. You need not to declare the pointer because it is not used. If you want to use pointers then you can write the function the following way

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
void GridWriter( int nGrid[10][10], int nOccupier )
{
        int ( *pnGrid )[10] = nGrid;
   
        for( int y = 0; y < 10; y++, pnGrid++ )
        {
            int *p = *pnGrid;
            for(int x = 0; x < 10; x++, p++ )
            {
                if(y < 5 && x < 5)
                {
                    *p = nOccupier;
                }
                else
                {
                    *p = 0;
                }

            }
        }
    }
Last edited on
Am I correctly understanding you to be saying that I only need int ( *pnGrid )[10] = nGrid; if I am going to use a pointer rather than subscriptors as you had? that simplifies things, but I still have a problem that is puzzling me.

I'm calling my function with the code:

GridWriter(nOccupierType, nAreaGrid[10][10]);

In the function definition I have:

void GridWriter( int nGrid[10][10], int nOccupier)

As you suggested. However my compiler returns the error:

No matching function to call to GridWriter(int&, int&)
candidates are: GridWriter(int (*) [10], int)


Am I calling to this function incorrectly? Thanks for the help so far.
closed account (zb0S216C)
How are you invoking the function?

Wazzak
I'm invoking it with:

GridWriter(nAreaGrid[10][10], nOccupierType);

before that I declare it with:

void GridWriter(int, int);

and the definition with parameters is:

void GridWriter( int nGrid[10][10], int nOccupier).
Last edited on
closed account (zb0S216C)
The two functions are not the same; you've just overloaded it. The prototype and definition should appear as:

1
2
3
4
5
6
7
8
9
//Prototype:
void GridWriter(int[10][10], int);
// The first parameter is secretly: int(*)[10][10].

// Definition:
void GridWriter(int a_[10][10], int occ_)
{
    // ...
}

Commisar Jimp wrote:
nAreaGrid[10][10]

You only need nAreaGrid here because the compiler already knows it's a matrix. However, you do need to pass the address of nAreaGrid.

Edit: Fixed errors in the code :)

Wazzak
Last edited on
Sorry it's taken me so long to reply here, I've been busy/frustrated. I thought I had been doing exactly as you guys suggested, and my code was still not working; then i realized I had misspelled a variable. Ugh. Everything's working fine now, thank you guys so much for the help and putting up with my steadfast refusal to understand what you were saying.
Topic archived. No new replies allowed.