pointer assigment

This is a discussion on pointer assigment within the C Programming forums, part of the General Programming Boards category; I have said 2d array of structures. each structure has a map.gridx and a map.gridy ... if i am at ...

  1. #1
    Registered User
    Join Date
    Dec 2003
    Posts
    41

    Lightbulb 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
    ~~~~~~~~~~
    Mitchell Kent
    07782383326
    spudtheimpaler@hotmail.com
    ~~~~~~~~~~

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>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.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Dec 2003
    Posts
    41

    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..
    Attached Files Attached Files
    ~~~~~~~~~~
    Mitchell Kent
    07782383326
    spudtheimpaler@hotmail.com
    ~~~~~~~~~~

  4. #4
    Registered User
    Join Date
    Dec 2003
    Posts
    41
    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...

    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);
    
    
    
    
    
    
    }
    ~~~~~~~~~~
    Mitchell Kent
    07782383326
    spudtheimpaler@hotmail.com
    ~~~~~~~~~~

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    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);
    [edit]
    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).
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    Registered User
    Join Date
    Dec 2003
    Posts
    41
    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
    Mitch
    ~~~~~~~~~~
    Mitchell Kent
    07782383326
    spudtheimpaler@hotmail.com
    ~~~~~~~~~~

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>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.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  8. #8
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Yes you can have a struct inside a struct.
    You can have an array of structs, or a struct with arrays inside

  9. #9
    Registered User
    Join Date
    Dec 2003
    Posts
    41
    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
    ~~~~~~~~~~
    Mitchell Kent
    07782383326
    spudtheimpaler@hotmail.com
    ~~~~~~~~~~

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 03-24-2008, 11:16 AM
  2. Parameter passing with pointer to pointer
    By notsure in forum C++ Programming
    Replies: 15
    Last Post: 08-12-2006, 08:12 AM
  3. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 04:36 AM
  4. How did you master pointers?
    By Afrinux in forum C Programming
    Replies: 15
    Last Post: 01-17-2006, 08:23 PM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 01:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21