(E-Mail Removed) wrote:

>

> 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.
/* BEGIN segregateXY.c output */

data[x][0] 0.000000 2.000000 4.000000 6.000000 8.000000 10.000000

data[x][1] 1.000000 3.000000 5.000000 7.000000 9.000000 11.000000

data_a[x] 0.000000 2.000000 4.000000 6.000000 8.000000 10.000000

data_b[x] 1.000000 3.000000 5.000000 7.000000 9.000000 11.000000

/* END segregateXY.c output */

/* BEGIN segregateXY.c */

#include<stdio.h>

#include<stdlib.h>

#define HUNDRED 6

void segregateXY(double data[HUNDRED][2], double *x, double *y)

{

size_t n = HUNDRED;

while (n-- != 0) {

x[n] = data[n][0];

y[n] = data[n][1];

}

}

int main(void)

{

size_t x, y;

double *data_a;

double *data_b;

double (*data)[2];

puts("/* BEGIN segregateXY.c output */\n");

data_a = malloc(HUNDRED * sizeof *data_a);

if (data_a == NULL) {

fputs("data_a == NULL\n", stderr);

exit(EXIT_FAILURE);

}

data_b = malloc(HUNDRED * sizeof *data_b);

if (data_b == NULL) {

free(data_a);

fputs("data_b == NULL\n", stderr);

exit(EXIT_FAILURE);

}

data = malloc(HUNDRED * sizeof *data);

if (data == NULL) {

free(data_a);

free(data_b);

fputs("data == NULL\n", stderr);

exit(EXIT_FAILURE);

}

for (x = 0; x != HUNDRED; ++x) {

for (y = 0; y != 2; ++y) {

data[x][y] = x + x + y;

}

}

fputs("data[x][0] ", stdout);

for (x = 0; x != HUNDRED; ++x) {

printf("%f ", data[x][0]);

}

fputs("\ndata[x][1] ", stdout);

for (x = 0; x != HUNDRED; ++x) {

printf("%f ", data[x][1]);

}

segregateXY(data, data_a, data_b);

fputs("\n\n data_a[x] ", stdout);

for (x = 0; x != HUNDRED; ++x) {

printf("%f ", data_a[x]);

}

fputs("\n\n data_b[x] ", stdout);

for (x = 0; x != HUNDRED; ++x) {

printf("%f ", data_b[x]);

}

free(data_a);

free(data_b);

free(data);

puts("\n\n/* END segregateXY.c output */");

return 0;

}

/* END segregateXY.c */

--

pete