Thread: passing dynamicaly allocated 2d arrays

  1. #1
    Registered User
    Join Date
    Nov 2004
    Posts
    55

    passing dynamicaly allocated 2d arrays

    Hi all,

    I'm writing a program where I am allocating a 2d array dynamically (ie using malloc), and then I want to pass this array to a function, but I'm not being successful...

    Here are the relevant parts of the program:
    Code:
    int main(void)
    {
            double **data;                        // 2d array to hold input file values
            const unsigned int ncols=29;   // number of columns to read
            unsigned int nrows=0;             // number of rows in input file
      
            /* allocate data array */
            **data=malloc(nrows*sizeof(*data));
            if (data==NULL) {
                    printf("Could not allocate data array of size %i\n",nrows*sizeof(*data));
                    return 1;
            }
            for (i=0; i<nrows; i++) {
                    data[i]=malloc(ncols*sizeof(*data[i]));
                    if (data[i]==NULL) {
                            printf("Could not allocate array data[%i] of size %i\n",i, ncols*sizeof(*data[i]));
                            return 1;
                    }
            }
    
    
            /* read data into data array */
            read_data(nrows, ncols, data);
            .
            .
            .
    }
    and the function header for read_data is:
    Code:
    void read_data(unsigned int rows, const unsigned int cols, double *data[][cols]);

    gcc (-Wall -pedantic) says:
    Code:
    lsis.least.sq.c:81: error: incompatible type for argument 3 of `read_data'
    I've tried combinations of *data[][ncols], **data, etc to use as my argument for read_data, but it doesn't work. I know that when passing multidimensional array, you need to define the last dimension, but I'm not sure why it doesn't work the ways I've been trying.

    Any sugggestions?

    Thanks
    Spiros

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Have you tried:
    Code:
    void read_data(unsigned int rows, const unsigned int cols, double **data);
    That is, the same type that data is.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    396
    Code:
    **data=malloc(nrows*sizeof(*data));
    Why are you dereferencing the pointer here?
    this affectation is incorrect (drop **).

    Concerning the function:
    Code:
    ...,double *data[][cols]);
    You cannot do that, 1st this is certainly not the same type than double** and the size you have to precise is the size of the 1st dimension. But the simplest solution here is to declare the third argument as a double** as you did for the variable.

  4. #4
    Registered User
    Join Date
    Jul 2005
    Posts
    31
    Damn! People in this board are just too damn efficient! Nobody had replied 5 mins ago when I first looked.

    Yes, all the above points...! The following doesn't return any errors with -Wall -pedantic

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void read_data(unsigned int rows, const unsigned int cols, double **data)
    {
    	;
    }
    
    int main(void)
    {
          double **data;                       /* 2d array to hold input file values */
          const unsigned long ncols=29;   	   /*  number of columns to read         */
          unsigned long nrows=0;               /*  number of rows in input file      */
    	  unsigned i;
    
          /* allocate data array */
          data = malloc(nrows*sizeof(double*));
          if (data==NULL) 
            {
              printf("Could not allocate data array of size %lu\n",nrows*sizeof(*data));
              return 1;
            }
    
          for (i=0; i<nrows; i++) 
            {
               data[i]=malloc(ncols*sizeof(*data[i]));
                  
    	       if (data[i]==NULL) 
                 {
                    printf("Could not allocate array data[%u] of size %lu\n",i, ncols*sizeof(*data[i]));
                    return 1;
                 }
             }
    
    
          /* read data into data array */
          read_data(nrows, ncols, data);
    
          return 0;
    }

  5. #5
    Registered User
    Join Date
    Nov 2004
    Posts
    55
    thank you for your replies, it works now.

    Can you please explain to me why do I need to call read_data using "data", wheras in the declaration of read_data it's "**data"?

  6. #6
    Registered User
    Join Date
    Jul 2005
    Posts
    31
    Hi,

    In the first line of the program you specify the type of data,

    Code:
    double **data;
    data is already a pointer to an array of pointers to doubles... which is what read_data requires it to be.

    The name is not '**data', the name is 'data' the type is 'double **'.

    Dan
    Last edited by boyfarrell; 05-25-2008 at 04:19 PM. Reason: Further explaination

  7. #7
    Registered User
    Join Date
    Nov 2004
    Posts
    55
    ah yes, it's very clear now

    thank you!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 2D arrays and storing characters
    By John_L in forum C Programming
    Replies: 4
    Last Post: 10-13-2007, 12:17 PM
  2. Arrays and pointers to allocated memory
    By TriKri in forum C Programming
    Replies: 19
    Last Post: 07-23-2006, 10:52 AM
  3. Having Trouble Passing typedef Arrays to a Function
    By jlharrison in forum C Programming
    Replies: 1
    Last Post: 03-27-2006, 12:06 PM
  4. Passing 2D arrays
    By samGwilliam in forum C++ Programming
    Replies: 1
    Last Post: 01-21-2006, 09:04 PM
  5. 2D Dynamically allocated pointer arrays
    By Lionmane in forum C Programming
    Replies: 37
    Last Post: 06-11-2005, 10:39 PM