Function for dynamic allocate two dimensional array in c

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • CzyToNieDariusz
    New Member
    • Apr 2010
    • 1

    #1

    Function for dynamic allocate two dimensional array in c

    Hi!

    I was looking a lot about that, but i didn't find any answer so I decided to ask here.

    I have a function:
    Code:
    void allocate(double **m,int n) {
         int i;
         m=(double**)malloc(n*sizeof(double *));
         for(i=0;i<n;i++)
    	  m[i]=(double*)malloc(n*sizeof(double));
    }
    and i want to write some values into the matrix so:
    Code:
    int main() {
    double ** m
    allocate(m,3);
    m[1][2]=2;             '\* Here I have a segmentation fault
    
    }
    somebody can help?
    thanks!
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    You never pass the pointer returned by malloc to the calling function.

    Read This

    Comment

    Working...