# Pointers to Arrays

Discussion in 'C Programming' started by joelperr@gmail.com, Mar 8, 2006.

1. ### Guest

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.

, Mar 8, 2006

2. ### mdlerGuest

Hello

Nice try to work with pointers

void segregateXY
(
double data[100][2],
int m,
double *X[], // error
double *Y[] // error
);

rather use
void segregateXY
(
double data[100][2],
int m,
double *X,
double *Y
);

next this declaration is no good
double x[m], y[m];
m is no constant

short use this

#define max_Rows 100
void segregateXY
(
double data[ max_Rows][2],
int m,
double *X,
double *Y
)
{
int row;

//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];
}
}
}

{
int rows=0;
double coordinates[ max_Rows][2];
double x[ max_Rows], y[ max_Rows];
// some function that filles arrary coordinates
segregateXY(coordinates, rows, x, y);
}

Greetings Olaf
schreef:

> 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.

mdler, Mar 8, 2006

3. ### Joe EstockGuest

note: message rearranged as as it *should* have been by the previous poster.

>>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.

>
>

mdler wrote:
> Hello
>
> Nice try to work with pointers
>
> void segregateXY
> (
> double data[100][2],
> int m,
> double *X[], // error
> double *Y[] // error
> );
>
> rather use
> void segregateXY
> (
> double data[100][2],
> int m,
> double *X,
> double *Y
> );

And exactly how is that giving the OP the same thing he was attempting
to do? double *X is a pointer to type double, whereas the OP was clearly
trying to do double **X.

[snip code not relevant to my reply]

> Greetings Olaf
> schreef:
>
>

Please do not top post as it only makes it more difficult for everyone

Joe

Joe Estock, Mar 8, 2006
4. ### peteGuest

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

pete, Mar 8, 2006
5. ### JpGuest

Thank you very much for the speedy reply. Although this is not quite
the implementation that I was aiming for, it gave me many good ideas to
better protect the memory allocation in my program in general.

Again, thank you for taking some of your time to help out. Much
appreciated!

Joel P.

Jp, Mar 8, 2006
6. ### JpGuest

Thank you Olaf,
This was exactly what I needed to correct the program. 10 keystrokes
and the problem was fixed.
Much obliged,

Joel P.

Jp, Mar 8, 2006
7. ### Default UserGuest

Jp wrote:

> Thank you very much for the speedy reply.

Please review the information below for futher posts in this newsgroup.

Brian
--
Please quote enough of the previous message for context. To do so from
Google, click "show options" and use the Reply shown in the expanded