# pointer assigment

• 03-01-2004
spudtheimpaler
pointer assigment
I have said 2d array of structures. each structure has a map.gridx and a map.gridy
...

if i am at map[5][9] ( map[5][9].gridx=5, and map[5][9].gridy=9 ) and a user wants to move north one square, ie change the y postion from 9 to 10, how is the best method of doing this? searching for a structure with gridx.(x position) gridy.(y position +1) or is there a way of doing it without actually having to acces the structure and just move the pointer.

ie if i pass into a move function:

Code:

``` location ** move(x position, y position) { map=map[x][y+1]; return map; }```
hope you know what I'm getting at.

prob is of course, the map pointer itself doesnt have the [x][y] at the end of it, so when i'm passing it in and out of all the other functions i need, , which is why im asking if i need to do it using the map.gridx and map.gridy elements of the structure.

whole code is attatched. and no i haven't got much further than last time :(

Many thanks.

Mitch
• 03-01-2004
Hammer
>>hope you know what I'm getting at.
No really :(

>>whole code is attatched.
No it's not ;)

>>map=map[x][y+1];
If you do this assignment, what point is going to point to the original map? If you want a pointer to a position, how about using a location* as a temporary pointer.

If you're having trouble, break the problem down into small, managable chunks. That way it's easier to see what you can and can't do.
• 03-01-2004
spudtheimpaler
oops
The code should be below this time.

Basically a pointer will start at point 0,0 on a 2d array. If a user wants to go East, it will move the pointer from 0,0 to 0,1. then if the user wants to go west the pointer will go back to 0,0

Its a mars rover simulation

It will be a function that is called often, and will change the position on the 2d array. then for example a status function will be called, and this will have to display the x and y position, a simulated power rating of the rover, the terrain type etc..
• 03-01-2004
spudtheimpaler
for example if i were to write some quick code as below, which should put out the "status" which include the current position of the rover, i have to relay the grid x and grid y to the output in this form:

map[x][y].gridx

as opposed to simply map.gridx

which says to me, i cant use map as just a pointer, if i want to always be aware of where i am within the 2d array, i have to search it every time i want to move and compare map[x][y].gridx to a current gridx int variable.

Is this the case or is there some C trickery i can use that i havent yet learnt?

I realise i may not be very clear in my description but i'm not very clear in my head... :confused:

Ta v much

Mitch

Code:

``` void status(int power) {                 location** map;                         printf("=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=");         printf("\n\n");                 printf("        Mars Rover Monitor\n\n");         printf("=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=");         printf("\n\nStatus: \n\n");         printf("Power: %d%\n\n",power);         printf("Position: (%d, %d)",map[0][0].Gridx, map[0][0].Gridy); }```
• 03-01-2004
Hammer
Code:

```typedef struct {   location  **map;   int      PosX;   int      PosY; } rover; void initRover(rover *myRover, location **map) {   myRover->map = map;   myRover->PosX = 0;   myRover->PosY = 0; } /* and in main: */ rover  myRover; location **map; map = make_map(mapsizex, mapsizey); initRover (&myRover, map);```

Get it? Add another layer in a the form of a structure representing the rover itself. So far you only have a map. If you were doing C++, I'd say think of the objects involved, "a robot has a map" is at least two objects (structures).
• 03-01-2004
spudtheimpaler
can you have a structure within a structure?

so far i have the map which has elements posX and PosY (GridX and GridY in my case)

But i can have the rover as another structure and then have the 2d array of structures *within* that structure?

I like it. I see what you mean, and am giving it a go.

Cheers very much :D
Mitch
• 03-01-2004
Hammer
>>can you have a structure within a structure?
That's what this is:
Code:

```typedef struct {   location  **map;   int      PosX;   int      PosY; } rover;```
The rover is the outer structure, and in it there's a map (or pointer to). "The rover has a map". Once you understand the relationships, it makes design easier.
• 03-01-2004
Thantos
Yes you can have a struct inside a struct.
You can have an array of structs, or a struct with arrays inside :)
• 03-01-2004
spudtheimpaler
Bonza. cheers.

Thanks a lot Hammer, it all makes sense, its just nt something i've ever done before and so not thought ... well possible i suppose.

I'm gonna play around with it for a while now. is myRover.map.gridx a possible syntax? dont answer, i'll play with the code myself for a while first.

Thanks a lot, really helped, makes a lot of sense.

And cheers Thantos, just starting to see how thats more often the case than not!

Sweet dreams

Mitch