Thread: 2d Array of structs(Help)

  1. #1
    Registered User
    Join Date
    Jan 2017
    Posts
    19

    2d Array of structs(Help)

    Hi. So I'm not sure why my program crashes when I run this code. Any help would be appreciated. Thanks

    Header file:
    Code:
    typedef struct
    {
     char token;
    
    }Grid;
    void initialize_grid(Grid *grid[][7]);
    void print_grid(Grid *grid[][7]);
    definitions:
    [code]

    void initialize_grid(Grid *grid[][7])
    {
    for (int y = 0; y < 6; y++)
    {
    for (int x = 0; x < 7; x++)
    grid[y][x]->token = '0';
    }
    }

    void print_grid(Grid *grid[][7])
    {
    for (int y = 0; y < 6; y++)
    {
    for (int x = 0; x < 7; x++)
    printf(" %c", grid[y][x]->token);
    printf("\n");
    }
    }

    [code/]

    main:
    Code:
    int main(void)
    {
     Grid grid[6][7];
     initialize_grid(&grid);
     print_grid(&grid);
    
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,260
    I am puzzled as to why you would call a structure consisting of a single char a "grid". Perhaps you had this in mind instead:
    Code:
    typedef struct
    {
        char token[6][7];
    } Grid;
    Now, you can simplify your Grid functions:
    Code:
    void initialize_grid(Grid *grid)
    {
        for (int y = 0; y < 6; y++)
        {
            for (int x = 0; x < 7; x++)
                grid->token[y][x] = '0';
        }
    }
    
    void print_grid(Grid *grid)
    {
        for (int y = 0; y < 6; y++)
        {
            for (int x = 0; x < 7; x++)
                printf(" %c", grid->token[y][x]);
            printf("\n");
        }
    }
    and then your main function would work almost as-is:
    Code:
    int main(void)
    {
        Grid grid;
        initialize_grid(&grid);
        print_grid(&grid);
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jun 2015
    Posts
    1,643
    As laserlight said, your code doesn't really make sense as is. There are three possibilities.

    1. laserlight's guess (and without your feedback, all we can do is guess!) is to take you at your word that Grid is a good name for the struct and put the whole grid of chars in the struct.

    2. Alternatively, you could get rid of the struct altogether and just use a 2D array of chars: char grid[ROWS][COLS]

    3. Finally, perhaps the struct was misnamed and should have been called something like Position. The idea here is that it will eventually have more than just a char in it. That code might look something like the following, which is similar to your original code. (The "\033..." part is for setting colors. This may not work on your system, in which case just remove those parts.)
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    #define ROWS 6
    #define COLS 7
    
    typedef struct {
      char token;
      int  weight;
    } Position;
    
    void initialize_grid(Position grid[][COLS]) {
      for (int r = 0; r < ROWS; r++)
        for (int c = 0; c < COLS; c++) {
          grid[r][c].token = '0';
          grid[r][c].weight = rand() % 2;  // random "weight" or 0 or 1
        }
    }
    
    void print_grid(Position grid[][COLS]) {
      for (int r = 0; r < ROWS; r++) {
        for (int c = 0; c < COLS; c++) {
          if (grid[r][c].weight > 0) printf("\033[34m"); // 34 is blue
          printf(" %c", grid[r][c].token);
          printf("\033[m"); // back to default color
        }
        printf("\n");
      }
    }
    
    int main(void) {
      srand((unsigned)time(NULL));
      Position grid[ROWS][COLS];
      initialize_grid(grid);
      print_grid(grid);
      return 0; 
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 12-20-2011, 09:43 PM
  2. Passing Structs Into An Array Of Structs.
    By TheTaoOfBill in forum C Programming
    Replies: 3
    Last Post: 10-07-2010, 09:38 AM
  3. array of structs
    By jeanluca in forum C Programming
    Replies: 2
    Last Post: 06-08-2009, 02:26 PM
  4. Replies: 3
    Last Post: 03-31-2009, 12:34 PM
  5. Replies: 12
    Last Post: 12-06-2005, 08:30 PM

Tags for this Thread