C. Segmentation error when a function modifies a 2d dynamically allocated array

I need a function that changes the given pointer to a 2d matrix as follows:

void intMatrixAll(int row, int col, int **matrix);

Now the function should allocate memory, and the matrix can be used. Rows and columns are specified at runtime.

#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 an error?

+3
source share
3 answers

Since modifying the matrix inside intMatrixAll () does not change testMatrix in main (). If you want to change the main variable, you need to pass a pointer to it. Therefore you need to change intMatrixAll to:

void intMatrixAll(int row, int col, int ***matrix)

intMatrixAll matrix *matrix ( , (*matrix)[...].

, intMatrixAll, :

intMatrixAll(testArrRow, testArrCol, &testMatrix);

, C pass-by-value, pass-by-value , .

, , .

+2

- NULL. intMatrixAll(). , testMatrix, .

#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);
      }
   }
}
+5

, - . , , , int intMatrixAll . , int , , testMatrix, NULL.

, , .. *, malloc d , testMatrix ,

. R. Samuel Klatchko .

, - , . int, .

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

, , , .

0

Source: https://habr.com/ru/post/1725902/


All Articles