Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Pointers to Arrays

Reply
Thread Tools

Pointers to Arrays

 
 
joelperr@gmail.com
Guest
Posts: n/a
 
      03-08-2006
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.

 
Reply With Quote
 
 
 
 
mdler
Guest
Posts: n/a
 
      03-08-2006
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
http://www.velocityreviews.com/forums/(E-Mail Removed) 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.


 
Reply With Quote
 
 
 
 
Joe Estock
Guest
Posts: n/a
 
      03-08-2006
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
> (E-Mail Removed) schreef:
>
>


Please do not top post as it only makes it more difficult for everyone
to follow along.

Joe
 
Reply With Quote
 
pete
Guest
Posts: n/a
 
      03-08-2006
(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
 
Reply With Quote
 
Jp
Guest
Posts: n/a
 
      03-08-2006
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.

 
Reply With Quote
 
Jp
Guest
Posts: n/a
 
      03-08-2006
Thank you Olaf,
This was exactly what I needed to correct the program. 10 keystrokes
and the problem was fixed.
Much obliged,

Joel P.

 
Reply With Quote
 
Default User
Guest
Posts: n/a
 
      03-08-2006
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
header.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
pointers, pointers, pointers... cerr C Programming 12 04-07-2011 11:17 PM
casting pointers/arrays to multidimensional arrays Francesco C++ 2 11-06-2009 09:04 AM
Multidimensional arrays and arrays of arrays Philipp Java 21 01-20-2009 08:33 AM
Learning pointers and arrays of pointers With Files ketema@gmail.com C Programming 1 03-28-2005 03:51 AM
Arrays and Pointers to Arrays kelvSYC C Programming 2 09-26-2003 06:52 AM



Advertisments