Memory and pointer allocation

Asked

Viewed 147 times

1

I’m implementing a Sudoku, but I’m having a problem filling it.

  #include <stdio.h>
  #include <stdlib.h>
  #include <stdbool.h>
  #include <math.h>
  #include <stdio_ext.h>

  typedef struct grid_info {
    int size;
    int fields_total;
    int* grid;
    int rows_mistakes;
    int column_mistakes;
    int sectors_mistakes;
  } grid;

  void welcome_screen(grid*);
  grid* fill_the_(grid*);

  int main(int argc, const char* argv[]) {
    grid* sudoku = (grid*)malloc(sizeof(grid));

    welcome_screen(sudoku);
    fill_the_(sudoku);

    printf("Sudoku size: %d\n", sudoku->size);
    free(sudoku);
    return 0;
  }

  grid* fill_the_(grid* sudoku) {
    sudoku->grid = malloc(sizeof(sudoku->fields_total));
    for(int row = 0; row < sudoku->size; row += (sudoku->fields_total / sudoku->size)) {
      for(int column = row; column = row + sudoku->size; column++) {
        scanf("%d", &sudoku->grid[column]);
      }
    }
  }

  void welcome_screen(grid* sudoku) {
    printf("Welcome to the sudoku game\n"
           "Please enter the number of sudoku: ");
    scanf("%d", &sudoku->size);
    while (sudoku->size < 2 || sudoku->size >= 10) {
      printf("The sudoku grid can only have size of 2 to 9\n"
             "Please enter the number of sudoku: ");
      scanf("%d", &sudoku->size);
    }
    sudoku->fields_total = pow(sudoku->size, 2);
  }

When filling in the struct, looks like he walks into a loop infinite, I don’t know if I did it right to get a index in grid within the struct.

  • I tried to make an answer, but it’s a bit confusing and complex the code I advise you to use matrices to represent rows and columns. Example of an array would be matrix[Row][col], and you create two is encapsulated, one to traverse the rows and one to traverse the columns. And you have a variable within the whole type structure called grid that has the same name as the grid structure.

2 answers

5


There are some problems, the code even compiles. I gave a good improved.

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

typedef struct grid_info {
    int size;
    int fields_total;
    int* grid;
    int rows_mistakes;
    int column_mistakes;
    int sectors_mistakes;
} grid;

void fill_the_(grid* sudoku) {
    sudoku->grid = malloc(sizeof(sudoku->fields_total));
    for (int row = 0; row < sudoku->size; row += (sudoku->fields_total / sudoku->size)) {
        for (int column = row; column < row + sudoku->size; column++) {
         scanf("%d", &sudoku->grid[column]);
        }
    }
}

void welcome_screen(grid* sudoku) {
    do {
        printf("The sudoku grid can only have size of 2 to 9\n"
             "Please enter the number of sudoku: ");
        scanf("%d", &sudoku->size);
    } while (sudoku->size < 2 || sudoku->size >= 10);
    sudoku->fields_total = pow(sudoku->size, 2);
}

int main(int argc, const char* argv[]) {
    grid* sudoku = malloc(sizeof(grid));
    welcome_screen(sudoku);
    fill_the_(sudoku);
    printf("Sudoku size: %d\n", sudoku->size);
    free(sudoku);
}

I put in the Github for future reference.

The main mistake was here: for(int column = row; column < row + sudoku->size; column++) {. In general comparisons in a for should be done until a certain state is reached. In general most used operators are >, <, >=, <=. Eventually the use of == and != and other expressions that generate a boolean can be used. But = is the assignment operator, and although it can be used there in certain situations, additionally, rarely, and especially in this case, is not appropriate.

3

The error is in that line:

for(int column = row; column = row + sudoku->size; column++)

The loop never ends because column is not being compared, just change to:

for(int column = row; column == row + sudoku->size; column++)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.