![]() |
| | #1 |
| Registered User Join Date: Nov 2004
Posts: 52
| passing dynamicaly allocated 2d arrays 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);
.
.
.
}
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' Any sugggestions? Thanks Spiros |
| s_siouris is offline | |
| | #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);
__________________ I might be wrong. Quote:
| |
| anon is offline | |
| | #3 |
| Registered User Join Date: Apr 2008
Posts: 282
| Code: **data=malloc(nrows*sizeof(*data)); this affectation is incorrect (drop **). Concerning the function: Code: ...,double *data[][cols]); |
| root4 is offline | |
| | #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 | |
| | #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 | |
| | #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; 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 | |
| | #7 |
| Registered User Join Date: Nov 2004
Posts: 52
| ah yes, it's very clear now thank you! |
| s_siouris is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
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 |