C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 05-25-2008, 03:14 PM   #1
Registered User
 
Join Date: Nov 2004
Posts: 52
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
s_siouris is offline   Reply With Quote
Old 05-25-2008, 03:30 PM   #2
The larch
 
Join Date: May 2006
Posts: 3,222
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.

Quote:
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).
anon is offline   Reply With Quote
Old 05-25-2008, 03:32 PM   #3
Registered User
 
Join Date: Apr 2008
Posts: 282
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.
root4 is offline   Reply With Quote
Old 05-25-2008, 03:53 PM   #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;
}
boyfarrell is offline   Reply With Quote
Old 05-25-2008, 03:59 PM   #5
Registered User
 
Join Date: Nov 2004
Posts: 52
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"?
s_siouris is offline   Reply With Quote
Old 05-25-2008, 04:17 PM   #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
boyfarrell is offline   Reply With Quote
Old 05-25-2008, 04:19 PM   #7
Registered User
 
Join Date: Nov 2004
Posts: 52
ah yes, it's very clear now

thank you!
s_siouris is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 11:48 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22