Store 2D grid in a struct?

Hello,

I'm currently doing my end of semester assignment where I have to code a robot to navigate a grid checking the squares to see if there's an object in them.

Anyway on one part of the assignment I need to store my data in a struct.
PDF of that Task: http://users.aber.ac.uk/owj3/PDF/map.pdf

I've declared the struct before the setup(); like this:

struct GridSquare{
int xpos; // current X axis position in grid
int ypos; // current Y axis position in grid
boolean checked; // Whether the square has been checked or not
boolean occupied; // Whether an object was detected or not

and then when the robot checks the squares it stores the data like this:

struct GridSquare target; // Make GridSquare struct to store data
target.xpos = xlinerequested; // Store the xposition the robot was sent to
target.ypos = ylinerequested; // Store the yposition the robot was sent to
target.occupied = IRResult; // Record whether anything was detected by the infra-red detector

Now that seems works all well and good, but when I try to make it an array like it says in the task PDF I keep getting compiler errors...

I've tried using "struct GridSquare[40][40]; // Create 1600 GridSquare objects to use as a map" before the setup(); to initialise but the compiler error I get is "expected unqualified-id before '[' token".

How would I go about changing the struct to an array so I can store the info about each square on the grid?

Thank you :slight_smile:

DarkGhost:
Now that seems works all well and good, but when I try to make it an array like it says in the task PDF I keep getting compiler errors...

If you post the code you're trying to compile we could try it for ourselves.

It's been awhile since I've used structures and 2D arrays but it sure looks like you're mixing the two incorrectly.

I don't think you want "struct GridSquare[40][40];"

The pdf doesn't use a 2D array here, I don't think you should either.

The type is struct GridSquare. You omitted the name of the array.

Looks like the instructor forgot how to do that, too.