-1

I want to multiply matrixes. I create matrix by malloc() function. Index (variable named i) shows NULL, instead of number

int** matrix_a = (int**)malloc(lines * sizeof(int*));
for (int i = 0; i < lines; i++) {
    if (i == NULL) 
        printf("'i' is NULL!");
    matrix_a[i] = (int*)malloc(trans * sizeof(int*));
}

Output:

'i' is NULL!

Error:

Unhandled exception at 0x00007FF6003C35BB in MPI_C.exe: 0xC0000005: Access violation writing location 0xFFFFFFFFABA57A00.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574

1 Answers1

1

It doesn't make sense to compare non-pointers to NULL. The NULL macro expands to an integer constant 0, possibly cast to (void *).

In the former case, your code just checks if (i == 0), which is true in the first iteration of the loop. In the latter case, you're doing if (i == ((void *)0)), which your compiler should be complaining about because it's a type error.

Only use NULL for pointer values, not numbers.

melpomene
  • 84,125
  • 8
  • 85
  • 148