What i need is a function that modifies given pointer to 2d matrix like this:
void intMatrixAll(int row, int col, int **matrix);
Now, a function should allocate memory and the matrix could be used. Rows and cols are given at run-time.
#include <stdio.h>
#include <stdlib.h>
#define PRINTINT(X) printf("%d\n", X);
void intMatrixAll(int row, int col, int **matrix);
int main(void) {
int testArrRow = 4;
int testArrCol = 6;
int **testMatrix = NULL;
intMatrixAll(testArrRow, testArrCol, testMatrix);
testMatrix[2][2] = 112; //sementation fault here :(
PRINTINT(testMatrix[2][2]);
system("PAUSE");
return 0;
}
void intMatrixAll(int row, int col, int **matrix) {
printf("intMatrixAll\n");
//allocate pointers:
matrix = malloc(row * sizeof(int *));
if(matrix == NULL) printf("Failed to allocate memmory.\n");
for(int i=0; i<row; i++) {
//allocate space for cols:
matrix[i] = malloc(col * sizeof(int));
if(matrix[i] == NULL) {
printf("Failed to allocate memmory for arr[%d].\n", i);
exit(0);
}
}
}
Why am i getting error?
Because modifying matrix inside intMatrixAll() does not modify testMatrix in main(). If you want to be able to modify main's variable, you need to pass a pointer to it. So you need to change intMatrixAll to:
void intMatrixAll(int row, int col, int ***matrix)
Inside intMatrixAll, you'll now need to change matrix
to *matrix
(and for when you are indexing it, you'll want (*matrix)[...]
.
Finally, you need to change your call to intMatrixAll to:
intMatrixAll(testArrRow, testArrCol, &testMatrix);
The reason why is that C only supports pass-by-value and pass-by-value does not support the called function changing the value of a variable in the caller.
In order to modify the value of a variable in the caller, you need to pass a pointer to the variable and then have the called function dereference it.
Test matrix is still NULL. You need to return the newly allocated pointer from intMatrixAll(). Either return the value from the function, or pass in the address of testMatrix so it can be set.
#include <stdio.h>
#include <stdlib.h>
#define PRINTINT(X) printf("%d\n", X);
void intMatrixAll(int row, int col, int **matrix);
int main(void) {
int testArrRow = 4;
int testArrCol = 6;
int **testMatrix = NULL;
intMatrixAll(testArrRow, testArrCol, &testMatrix);
testMatrix[2][2] = 112; //sementation fault here :(
PRINTINT(testMatrix[2][2]);
system("PAUSE");
return 0;
}
void intMatrixAll(int row, int col, int ***matrix) {
printf("intMatrixAll\n");
//allocate pointers:
*matrix = malloc(row * sizeof(int *));
if(*matrix == NULL) printf("Failed to allocate memmory.\n");
for(int i=0; i<row; i++) {
//allocate space for cols:
*matrix[i] = malloc(col * sizeof(int));
if(*matrix[i] == NULL) {
printf("Failed to allocate memmory for arr[%d].\n", i);
exit(0);
}
}
}
Please see here for a discussion on something similar. The reason it seg faulted is because you are not passing the pointer-to-pointer of int's back out of the function intMatrixAll
to the main routine. In other words, you are passing in a parameter of double pointer to int's by value, not by reference, and tried to access testMatrix
when it was in fact still NULL
.
So you need to add another level of indirection, i.e. *
and use that indirection as a means to alter the double pointers to be malloc
d and for the main routine to see that testMatrix
has indeed being allocated.
See R. Samuel Klatchko's answer above.
As a general rule, if you want to pass a pointer to something as a parameter by reference, add another level of indirection. In your case, you pass in a double pointer to int, make the parameter as a triple pointer in the function.
int **testMatrix;
void intMatrixAll(int row, int col, int ***matrix){
// In here use this (*matrix)
}
// Then the calling of the function would look like this
intMatrixAll(testArrRow, testArrCol, &testMatrix);
// Note the ampersand above to treat this double pointer passed in by reference
Hope this helps and make sense,
Best regards,
Tom.