Define a 2D array and pass it in a function

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • casybay
    New Member
    • Oct 2007
    • 35

    Define a 2D array and pass it in a function

    Hi all,

    I would like to ask for you help to correct the following code for me. I am trying to define a 2D int array, then pass it in a function. I got an error "error: a value of type "void *" cannot be used to "xx file", line 113". Here is my code:
    Code:
       .......
        void fun(int**);
    ......
    ///////////////////////////////
       int main(int argc, char** argv){
    .......
        //the following is line 113
        int **distance = malloc(n*sizeof(int *)),**trace = malloc(n*sizeof(int *));
        distance = trace = malloc(n * sizeof(int *));
        for (i = 0; i < m; i++) {
            distance[i] = trace[i] = malloc(m * sizeof(int));
         }
    .......
        fun(distance);
       }
    //////////////////////////////
      void fun(int **distance){
    .....
             for(int i=0;i<n;i++){
                for(int j=0;j<m;j++){
                   distance[i][j]=distance[i][j]*2;
               } 
            }
    ......
       }
    //////////////////////////////
    Thx!!!!
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Read this: http://www.thescripts.com/forum/thread772412.html.

    What you want is at the end of the article.

    Comment

    Working...