J
joelperr
Hello,
I am attempting to separate a two dimensional array into two
one-dimensional arrays through a function. The goal of this is that,
from the rest of the program, a data file consisting of two columns of
coordinate data is read and stored into a 2D array. From this 2D array
I would like to split it into two 1D arrays, consisting of x and y
components, respectively. However, to return the two arrays from the
function I am utilizing pointers, and thus this becomes pointers to
arrays... I am unsure as how to implement this, and what I have tried
has failed:
/* Return values of x,y from a 2D array as two new 1D arrays using
pointers */
void segregateXY(double data[100][2], int m, double *X[], double *Y[]
){
int row, cols;
double x[m], y[m];
//m is the maximum number of coordinate pairs I want to allow
if (m > 100)
printf("\nDatafile too large...");
else {
for(row=0; row<m; row++) {
*X[row] = data[row][0];
*Y[row] = data[row][1];
}
}
}
And from the main i call:
/* Initialize 2D array */
double coordinates[rows][cols]; //rows, cols are initialized
earlier...
/* Read successive lines of data from spedified file, ending at
EOF marker */
int p=0;
while (fscanf(fin, "%lf %lf", &coordinates[p][0],
&coordinates[p][1]) != EOF) {
p++;
}
double x[rows], y[rows];
segregateXY(coordinates, rows, &x, &y);
Now I know that the data is loaded correctly into the matrix (from
debugging), but I can't for the life of me extract the two 1D arrays
from the 2D array. I always get this error (or worse):
MLS.c:63: warning: passing arg 3 of `segregateXY' from incompatible
pointer type
MLS.c:63: warning: passing arg 4 of `segregateXY' from incompatible
pointer type
Any help or suggestions would be welcome.
Joel P.
I am attempting to separate a two dimensional array into two
one-dimensional arrays through a function. The goal of this is that,
from the rest of the program, a data file consisting of two columns of
coordinate data is read and stored into a 2D array. From this 2D array
I would like to split it into two 1D arrays, consisting of x and y
components, respectively. However, to return the two arrays from the
function I am utilizing pointers, and thus this becomes pointers to
arrays... I am unsure as how to implement this, and what I have tried
has failed:
/* Return values of x,y from a 2D array as two new 1D arrays using
pointers */
void segregateXY(double data[100][2], int m, double *X[], double *Y[]
){
int row, cols;
double x[m], y[m];
//m is the maximum number of coordinate pairs I want to allow
if (m > 100)
printf("\nDatafile too large...");
else {
for(row=0; row<m; row++) {
*X[row] = data[row][0];
*Y[row] = data[row][1];
}
}
}
And from the main i call:
/* Initialize 2D array */
double coordinates[rows][cols]; //rows, cols are initialized
earlier...
/* Read successive lines of data from spedified file, ending at
EOF marker */
int p=0;
while (fscanf(fin, "%lf %lf", &coordinates[p][0],
&coordinates[p][1]) != EOF) {
p++;
}
double x[rows], y[rows];
segregateXY(coordinates, rows, &x, &y);
Now I know that the data is loaded correctly into the matrix (from
debugging), but I can't for the life of me extract the two 1D arrays
from the 2D array. I always get this error (or worse):
MLS.c:63: warning: passing arg 3 of `segregateXY' from incompatible
pointer type
MLS.c:63: warning: passing arg 4 of `segregateXY' from incompatible
pointer type
Any help or suggestions would be welcome.
Joel P.