Dynamically allocating a 2D array in C

Jude

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;
}
artm

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

c: issues when allocating 2d char array dynamically?

From Dev

C segmentation fault when dynamically allocating 2d array

From Dev

dynamically allocating my 2d array in c

From Dev

Dynamically allocating a 2D string array

From Dev

Allocating 2D array dynamically

From Dev

Dynamically Allocating 2D Arrays in C

From Dev

Dynamically allocating an array in a function in C

From Dev

C - Dynamically allocating array for stdin without realloc

From Dev

Dynamically allocating array of struct from file C

From Dev

Allocating an 2D array in C in heap instead of stack

From Dev

Allocating a 2d Array

From Dev

Dynamically allocating array explain

From Dev

Dynamically Allocating Array With Datafile

From Dev

Dynamically allocating an array of a struct

From Dev

Dynamically allocating threads in C

From Dev

Dynamically allocating 1D array of structs : Two Methods

From Dev

Dynamically input 2d character array in C++

From Dev

How to dynamically allocate a 2D array in C++?

From Dev

Dynamically creating a 2D array of pointers using malloc in C

From Dev

Matrix of chars (dynamically allocated 2D array) in c

From Dev

Allocating 2D array with pointer to fixed-size array

From Dev

dynamically allocating an int array inside a structure

From Dev

Trouble dynamically allocating memory for string array

From Dev

Allocating memory for array of pointers of int dynamically

From Dev

Allocating a 2D contiguous array within a function

From Dev

Dynamically allocating memory for changing array size starting with unknown size C++

From Dev

C Programming: Reading data from a file, dynamically allocating memory, placing contents in struct array

From Dev

when allocating memory for an array dynamically (in C), what does the (int *) cast do?

From Dev

Allocating an array of a class c++

Related Related

  1. 1

    c: issues when allocating 2d char array dynamically?

  2. 2

    C segmentation fault when dynamically allocating 2d array

  3. 3

    dynamically allocating my 2d array in c

  4. 4

    Dynamically allocating a 2D string array

  5. 5

    Allocating 2D array dynamically

  6. 6

    Dynamically Allocating 2D Arrays in C

  7. 7

    Dynamically allocating an array in a function in C

  8. 8

    C - Dynamically allocating array for stdin without realloc

  9. 9

    Dynamically allocating array of struct from file C

  10. 10

    Allocating an 2D array in C in heap instead of stack

  11. 11

    Allocating a 2d Array

  12. 12

    Dynamically allocating array explain

  13. 13

    Dynamically Allocating Array With Datafile

  14. 14

    Dynamically allocating an array of a struct

  15. 15

    Dynamically allocating threads in C

  16. 16

    Dynamically allocating 1D array of structs : Two Methods

  17. 17

    Dynamically input 2d character array in C++

  18. 18

    How to dynamically allocate a 2D array in C++?

  19. 19

    Dynamically creating a 2D array of pointers using malloc in C

  20. 20

    Matrix of chars (dynamically allocated 2D array) in c

  21. 21

    Allocating 2D array with pointer to fixed-size array

  22. 22

    dynamically allocating an int array inside a structure

  23. 23

    Trouble dynamically allocating memory for string array

  24. 24

    Allocating memory for array of pointers of int dynamically

  25. 25

    Allocating a 2D contiguous array within a function

  26. 26

    Dynamically allocating memory for changing array size starting with unknown size C++

  27. 27

    C Programming: Reading data from a file, dynamically allocating memory, placing contents in struct array

  28. 28

    when allocating memory for an array dynamically (in C), what does the (int *) cast do?

  29. 29

    Allocating an array of a class c++

HotTag

Archive