Thread: pointers and structures

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    6

    Post pointers and structures

    /*fill a m x n grid with 0 or 1 */
    I'm trying to return a pointer to the cell on the grid but I'm not getting the correct output..please help me figure out what I'm doing wrong.
    Code:
    typedef enum
    {
        a,
        b
    } cell_alpha;
    Code:
    typedef struct
    {
        int rows;
        int columns;
        /* Pointer to the beginning of the grid cell data where the cells are in a row-major layout */
        cell_alpha * cells;
    } grid;
    need help with the following code:

    Code:
    cell_alpha * abc_cell(grid * present_grid, int row, int column)
    {
      return &cell_alpha *(row*(grid->rows)+column); /*! Return a pointer to the corresponding cell on the grid. */
    }
    Last edited by Jumgle; 04-16-2011 at 08:57 PM.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    An "m x n grid" is an array, not a struct.
    Code:
    int grid[m][n];
    You address the cells as (for example)... grid[1][7] ... which can be done very easily in loops...
    It's all very civilized...

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    6
    Actually, i want to store info about different types of gird kind of like a board say if i want to implement a chess board with current state of the board, next state of the board etc. and store that info using struct and my question is how to return a pointer(correct syntax) to a particular cell on the grid that has the coordinates given(row, column and the grid info).

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    You don't need pointers for this...

    If you're wanting say three states of a chess board... past, present and future... 0, 1 & 2 respectively you can do this...

    Code:
    #define states 3        
    #define rows 8
    #define columns 8
    
    int board[states][rows][columns];
    The actual data for each cell is addressable by using the cell's coordinates... board[0][2][3] ... which would be past tense, three over and four down.

    Lets take the example that you want to assign a priority level 10 to a future square in the 5th row, 6th column...
    Code:
    board[2][4][5] = 10;
    These numbers are offset by one because C starts arrays at 0, not 1... so the 5th row is numbered 4...

    Similarly to test the priority level of a move to that square you could do this...
    Code:
     
    if (board[2][4][5] > 9)
      MoveNow(4,5); // take the square.
    It does everything you want... you just need to suss out how arrays store data.
    Last edited by CommonTater; 04-16-2011 at 10:35 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Structures, and pointers to structures
    By iloveitaly in forum C Programming
    Replies: 4
    Last Post: 03-30-2005, 06:31 PM
  2. structures with pointers to structures
    By kzar in forum C Programming
    Replies: 3
    Last Post: 11-20-2004, 09:32 AM
  3. Pointers to Classes || pointers to structures
    By C++Child in forum C++ Programming
    Replies: 24
    Last Post: 07-30-2004, 06:14 PM
  4. pointers and structures
    By coo_pal in forum C Programming
    Replies: 1
    Last Post: 07-23-2003, 04:45 AM
  5. Replies: 5
    Last Post: 04-11-2002, 11:29 AM