Variable value changing after returning from a function in C -


i've been coding university working matrices , can't find error in code changing value of variable save columns of matrix. i've tried debugging , can´t find it, ends function allocate memory por matrix , enters next function (which gets values keyboard fill matrix) wrong column value. code next:

#include <stdio.h> #include <stdlib.h> #define debug 1  void allocate (int ***mat,int n,int m){     int i;     *mat = (int **) malloc (n*sizeof(int*));     (i=0; i<n; i++){         mat[i] = (int *) malloc (m*sizeof(int));     }     #if debug         printf ("allocate n: %d m: %d\n",n,m);     #endif // debug }  void initialize (int **mat, int n, int m){     int i,j;     #if debug         printf ("initialize n: %d m: %d\n",n,m);     #endif // debug     (i=0; i<n; i++){         (j=0; j<m; j++){             printf ("enter value position [%d][%d]: ",i,j);             scanf ("%d",&(mat[i][j]));         }     } }  int main() {         int n=2;         int m=3;         int **mat=null;         #if debug             printf ("before allocate n: %d m: %d\n",n,m);         #endif // debug         allocate (&mat,n,m);          #if debug             printf ("after allocate n: %d m: %d\n",n,m);         #endif // debug         initialize (mat,n,m);         return 0; } 

so if run debug set 1 values of n , m (which rows , columns). using codeblocks. time!

update function

void allocate( int ***mat, int n, int m ) {     int i;      *mat = (int **) malloc( n * sizeof( int* ) );     ( = 0; < n; i++ )     {         ( *mat )[i] = ( int *) malloc ( m * sizeof( int ) );     }     #if debug         printf ("allocate n: %d m: %d\n",n,m);     #endif // debug } 

Comments

Popular posts from this blog

PHP DOM loadHTML() method unusual warning -

python - How to create jsonb index using GIN on SQLAlchemy? -

c# - TransactionScope not rolling back although no complete() is called -