Memory Allocation and Pointers

1

I'm implementing a Sudoku, but I'm having a hard time filling it out.

  #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);
  }

At the time of populating struct , it looks like it enters an infinite loop , I do not know if I did it right to get an index on the grid within struct .

    
asked by anonymous 03.09.2015 / 06:11

2 answers

5

There are some problems, the code does not even compile. I have made a good improvement.

#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);
    return 0;
}

The main error was here: for(int column = row; column < row + sudoku->size; column++) { . In general comparisons in% w /% must be made until it reaches a certain state. In general the most used operators are for , > , < , >= . 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.

    
03.09.2015 / 09:32
3

The error is in this 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++)
    
03.09.2015 / 08:38