1

I've been reading around and I've been applying what I've been reading to my code but I am not sure if I am missing something.. the 2d array is suppose to mirror sudoku. I know the problem area is in my arrayMake function. My professor recommended using a cast with the malloc call so: sudoku = (int**)malloc(sudokus*sizeof(int*)); but that did not work for me.

int main(){
    int sudokus;
    int** sudoku;
    sudokus = getUserInfo();
    sudoku = arrayMake(sudokus);
    /*for (int i = 0; i < (SIZE*sudokus), i++;){
        for (int j = 0; j < SIZE, j++;){
            printf("Numbers[%d][%d]:%d", i, j, sudoku[i][j]);
        }
    }*/
    system("pause");
    return 0;
}

int getUserInfo(){
    int sudokus;
    printf("How many Sudokus are you checking today?\n");
    scanf("%d{^\n]\n", &sudokus);

    return sudokus;
}

int** arrayMake(int sudokus){
    int **sudoku;
    int realsize;
    realsize = 9 * sudokus;

    sudoku = malloc(realsize*sizeof(int*));
    if (sudoku == NULL){
        printf("Memory allocation failed");
        return 0;
    }
    for (int i = 0; i < realsize, i++;){
        sudoku[i] = malloc(9 * sizeof(int));

        if (sudoku[i] == NULL){
            printf("Memory allocaiton failed");
            return 0;
                            }

    }

    return sudoku;
}
8
  • Do you want to allocate an array of pointers or an array of ints? Commented Jan 25, 2016 at 0:29
  • 1
    To allocate a 2D array of ints, just allocate the number of rows times the number of columns times sizeof(int). You don't need to make a new allocation for each row or each column. Commented Jan 25, 2016 at 0:32
  • 1
    what is your realsize ? Do you mean it's always as realsize = 9 * sudokus; ? Commented Jan 25, 2016 at 0:34
  • 1
    So what exactly is going wrong? Commented Jan 25, 2016 at 0:38
  • 2
    All your for loops are screwy. for (int i = 0; i < realsize, i++;) should be for (int i = 0; i < realsize; i++) Commented Jan 25, 2016 at 0:39

3 Answers 3

3

My professor recommended using a cast with the malloc call so: sudoku = (int**)malloc(sudokus * sizeof(int*)); but that did not work for me.

To dynamically allocate for 2D array, you usually need to do two steps. Your code is not clear as you include a realsize = 9 * sudokus which doesn't make sense. Anyway, for simplicity, lets assume your sudoku is a 3x3 matrix. You'll need to:

  1. Allocate for the pointer to pointer to int:

    int **sudoku = malloc( 3 * sizeof( int * ) );
    
  2. Allocate for each of the individual pointer to int:

    for( int i = 0; i < 3; i++ )
        sudoku[i] = malloc( 3 * sizeof( int ) );
    
Sign up to request clarification or add additional context in comments.

3 Comments

Alright so that code made sense, the reason I had realsize is because the user should be able input the number of sudoku grids (so how many 9x9 grids) he is going to input into the program. I was trying to find a way to extend the amount of rows, while keeping 9 columns. I assume you do this by changing the first malloc call?
You need to change both malloc calls to 9 instead of 3.
Algiht yea Its all working fine now even got it going with calloc, thanks!
2

From what I see your problem exists in your for loops where you have:

for (i = 0;i < realsize , i++)

when you really meant:

for (i = 0;i < realsize ; i++) ^ Note the change of , to ;

Comments

0

scanf("%d{^\n]\n", &sudokus); is a mistake.

I guess you meant the { to actually be a [ but the format string is still wrong even after that change. I think you intended to consume the rest of the input, up to and including a newline character. However, your format string does not actually do that.

Scanf'ing for \n actually means consume any amount of whitespace, so in fact this code (with the [ fix) would continue waiting for input until there was a newline, and also another non-whitespace character typed after the newline.

Better would be:

scanf("%d", &sudokus);
int ch;
while ( (ch = getchar()) != '\n' && ch != EOF ) { }

There are a few different ways to achieve the same goal. (Note that scanning for %d[^\n]%c is not one of them; that string is also broken).

Also I would suggest a different variable name than sudokus. It's confusing having two similarly-named variables sudoku and sudokus. Name it something that reflects its meaning.


For allocating your array, it would be much simpler to take out the arrayMake function and write something like:

int sudoku[9][9];

(I couldn't figure out what sudokus was supposed to mean or what realsize was going to be, but you could put your intended dimension inside the square brackets there).

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.