-3

I'm trying to create a 2D random integer matrix and while inserting a number I keep getting 'dereferencing NULL pointer' error which keeps crashing the program.

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

int** createMatrix(int, int);
void randomIntegerMatrix(int**, int, int);
void printMatrix(int**, int, int);
void freeMemory(int**, int, int);

int main()
{
    const int cols = 10;
    const int rows = 20;
    int** matrix = createMatrix(rows, cols);
    randomIntegerMatrix(matrix, rows, cols);
    printMatrix(matrix, rows, cols);
    freeMemory(matrix, rows, cols);
}
// generating array
int** createMatrix(int rows, int cols) 
{
    int** matrix = (int**)malloc(rows * sizeof(int*));
    for (int i = 0; i < rows; i++) {
        matrix[i] = (int*)malloc(cols * sizeof(int));
    }
    return matrix;
}

// inserting elements to an array
void randomIntegerMatrix(int** matrix, int rows, int cols)
{
    srand(time(NULL));
    for (int i = 0; i < rows; i++) 
    {
        for (int j = 0; j < cols; j++)
        {
            matrix[i][j] = rand() % 1000; 
        }
    }
}

void printMatrix(int** matrix, int rows, int cols)
{
    for (int i = 0; i < rows; i++)
    {
        for (int j = 0; j < cols; j++)
        {
            printf("%d ", matrix[i][j]);
        }
        printf("\n");
    }
}
void freeMemory(int** matrix, int rows, int cols) 
{
    for (int i = 0; i < rows; i++) {
        free(matrix[i]);
    }
    free(matrix);
}

How do I fix it? //EDIT: Updated with everything pointed out in replies.A functioning code.

ebeebe
  • 5
  • 4
  • 3
    Please try to create a [mre], and [edit] your question to show it. – Some programmer dude May 10 '22 at 16:51
  • 1
    Also, since you seem to know exactly when and where the crash happens, I assume you have used a debugger to catch the crash and locate it in your source? Then if you check the values of `rows` and `cols` as well a `i` and `j`, what are the values of those variables? What are, indeed, the values of `matrix` and `matrix[i]`? – Some programmer dude May 10 '22 at 16:53
  • Probably should read this: [Correctly allocating multi-dimensional arrays](https://stackoverflow.com/questions/42094465/correctly-allocating-multi-dimensional-arrays). And I don't mean skim it looking for scripted solution code you can scrape without the least bit of understanding. *read it*. – WhozCraig May 10 '22 at 17:12
  • 1
    By the way, in C you [don't have to (and really shouldn't) cast the result of `malloc`](https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc/). – Some programmer dude May 10 '22 at 17:27
  • This is not the program that crashes. This one cannot crash because it cannot run. It has no `main` function. We cannot help fixing a thing we don't get a chance to see. But one suggestion can be made. *Avoid globals*. Pass parameters to functions instead. `createMatrix` accepts `rows` and `cols` as parameters. Why not `randMatrix`? – n. 1.8e9-where's-my-share m. May 10 '22 at 19:29
  • @Someprogrammerdude I was taught to use `malloc` I used, but when I try the malloc casting you sent `int **matrix = malloc(sizeof *matrix * rows);` I get `malloc error: a value of type "void *" cannot be used to initialize an entity of type "int **"`. – ebeebe May 10 '22 at 19:31
  • 2
    @ebeebe That would mean you're compiling your code as C++. If it's C code, don't do that. – dbush May 10 '22 at 19:37
  • Where's the `printMatrix` function? The code posted should be *exactly* what you're compiling and running. – dbush May 10 '22 at 19:49
  • @dbush I added it, but the problem I had is already solved and was caused by rand() function as I used srand() the wrong way. – ebeebe May 10 '22 at 20:05
  • Still can't be compiled. No include statements. Functions not defined before they are used. `srand()` still can't take `NULL` as an argument. Where is what you ran? _Copy and paste the entire program that you actually compiled and ran._ This is not a hard concept. – Mark Adler May 10 '22 at 20:17
  • @MarkAdler Sorry, I changed it in my code, just didn't paste it here. – ebeebe May 10 '22 at 20:46
  • Good. Now the code compiles. _And_ it works fine! There is no dereference error, no out of bounds error, nothing wrong at all that I can see. (The only minor nit is that `freeMemory()` doesn't need or use `cols`. So that can be removed. Also, the matrix would look nicer if you used `%4d` instead of `%d`.) – Mark Adler May 10 '22 at 22:21
  • As @dbush says, you're building a C++ program, not C. What is the name of your source file? Does it end in `.c` or e.g. `.cpp`? – Some programmer dude May 11 '22 at 05:30
  • 1
    Also, don't update your questions with the working code, that makes the question kind of worthless since your code doesn't have the fault you're asking about. Especially bad if there's plenty of comments and even worse if there's an answer. If the posted answer doesn't answer your question or solve your problem, comment on the answer. If you find the solution and it is different from the posted answers, post your own answer instead. Otherwise if an answer solves your problem *accept* it. – Some programmer dude May 11 '22 at 05:32

0 Answers0