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:
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;
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
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.
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.