472,986 Members | 2,905 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,986 software developers and data experts.

passing a pointer to a 2D array to a function!!!!

Hello,

In my code I have a large static 2D arrays defined as:

code:

Expand|Select|Wrap|Line Numbers
  1. #define LONMAX 1440
  2. #define LATMAX60 480
  3.  
  4. void main (int argc, char *argv[])
  5. {
  6.      .......
  7.     float precip60[LATMAX60][LONMAX];
  8.     float error60[LATMAX60][LONMAX];
  9.     char source60[LATMAX60][LONMAX];
  10.  
I'm passing these arrays to other function as address, reading them in the other function as pointers and then operate on them
so in order not to pass the whole large array to another large array in the function I thought I just pass the address to a pointer.
code:

Expand|Select|Wrap|Line Numbers
  1.     nread = read3B4XRT2B (LATMAX60, fp, precip);
  2.     nread = read3B4XRT2B (LATMAX60, fp, error);
  3.     nread = read3B4XRT1B (LATMAX60, fp, source);
  4.  
  5.     .......
  6.  
  7. }
  8.  
  9. int read3B4XRT2B (
  10.     int latmax,
  11.     FILE *fp,
  12.     float *out)
  13. {
  14.     int row, col;
  15.     int nread;
  16.     short int buffer[LONMAX];
  17.     for (row=0; row<latmax; row++)
  18.     {
  19.         nread = fread (buffer, 2, LONMAX, fp);
  20.         for (col=0; col<LONMAX; col++)
  21.         {
  22.             if (buffer[col] == I2MISSING)
  23.                 *(*(out+row)+col) = FMISSING;
  24.             else
  25.                 *(*(out+row)+col) = buffer[col] / SCALE;
  26.         }
  27.     }
  28.     return nread;
  29. }
problem: this expression: *(*(out+row)+col) = FMISSING;
is not compiled
compiler error message:
In function read2B4XRT2B Invalid type argument of `unary*'

I do not understand why this code is not working, I'm passing a 2D array as an address to another function as a pointer then storing data in the content pointed to by that pointer.

I hope someone can help me with that code.
Thanks in advance.
mshaaban
Jun 8 '07 #1
5 2369
mac11
256 100+
Hello,

In my code I have a large static 2D arrays defined as:

code:

#define LONMAX 1440
#define LATMAX60 480

void main (int argc, char *argv[])
{
.......
float precip60[LATMAX60][LONMAX];
float error60[LATMAX60][LONMAX];
char source60[LATMAX60][LONMAX];


I'm passing these arrays to other function as address, reading them in the other function as pointers and then operate on them
so in order not to pass the whole large array to another large array in the function I thought I just pass the address to a pointer.
code:

nread = read3B4XRT2B (LATMAX60, fp, precip);
nread = read3B4XRT2B (LATMAX60, fp, error);
nread = read3B4XRT1B (LATMAX60, fp, source);

.......

}

int read3B4XRT2B (
int latmax,
FILE *fp,
float *out)
{
int row, col;
int nread;
short int buffer[LONMAX];
for (row=0; row<latmax; row++)
{
nread = fread (buffer, 2, LONMAX, fp);
for (col=0; col<LONMAX; col++)
{
if (buffer[col] == I2MISSING)
*(*(out+row)+col) = FMISSING;
else
*(*(out+row)+col) = buffer[col] / SCALE;
}
}
return nread;
}

problem: this expression: *(*(out+row)+col) = FMISSING;
is not compiled
compiler error message:
In function read2B4XRT2B Invalid type argument of `unary*'

I do not understand why this code is not working, I'm passing a 2D array as an address to another function as a pointer then storing data in the content pointed to by that pointer.

I hope someone can help me with that code.
Thanks in advance.
mshaaban
you must change
Expand|Select|Wrap|Line Numbers
  1. int read3B4XRT2B (
  2.     int latmax,
  3.     FILE *fp,
  4.     float *out)
to something like this
Expand|Select|Wrap|Line Numbers
  1. int read3B4XRT2B (
  2.     float** latmax,
  3.     FILE *fp,
  4.     float *out)
and then change this
Expand|Select|Wrap|Line Numbers
  1.                 *(*(out+row)+col) = FMISSING;
to something like
Expand|Select|Wrap|Line Numbers
  1. latmax[row][col] = FMISSING;
I'm sure this isn't exactly what you want but it should point you in the right direction.

Your compiler is barking at the '*' because your trying to use pointer dereferencing (such as *pointer = value) but there is nothing being dereferenced (you have no pointer next to the '*'

Hope this helps!
Jun 9 '07 #2
weaknessforcats
9,208 Expert Mod 8TB
You have a problem with how arrays work.

You are passing precip to your function.

By definition, the name of an array is the address of element 0.

Element 0 of precip is and array of LONGMAX floats.

So, the name precip is the address of an array of LONGMAX floats:
Expand|Select|Wrap|Line Numbers
  1. float (*ptr)[LONGMAX] = precip;  /* OK */
  2.  
Your function has a float*. This does not match the code above, hence the compiler error.

Your solution in this case is to call the function with &precip[0][0], which is a float* and you should be fine.
Jun 9 '07 #3
you must change
Expand|Select|Wrap|Line Numbers
  1. int read3B4XRT2B (
  2.     int latmax,
  3.     FILE *fp,
  4.     float *out)
to something like this
Expand|Select|Wrap|Line Numbers
  1. int read3B4XRT2B (
  2.     float** latmax,
  3.     FILE *fp,
  4.     float *out)
and then change this
Expand|Select|Wrap|Line Numbers
  1.                 *(*(out+row)+col) = FMISSING;
to something like
Expand|Select|Wrap|Line Numbers
  1. latmax[row][col] = FMISSING;
I'm sure this isn't exactly what you want but it should point you in the right direction.

Your compiler is barking at the '*' because your trying to use pointer dereferencing (such as *pointer = value) but there is nothing being dereferenced (you have no pointer next to the '*'

Hope this helps!
Hello,
Thanks for your reply.
I have a question, in your code; the variable "latmax" what does it have to do with anything did you mean the variable "out"?? so the code should be:

Code:

float** out;

out[row][col] = FMISSING;


Thanks for your help.
Regards,
mshaaban
Jun 9 '07 #4
AdrianH
1,251 Expert 1GB
Hello,
Thanks for your reply.
I have a question, in your code; the variable "latmax" what does it have to do with anything did you mean the variable "out"?? so the code should be:

Code:

float** out;

out[row][col] = FMISSING;


Thanks for your help.
Regards,
mshaaban
Yes, that is most likely what s/he meant.


Adrian
Jun 10 '07 #5
AdrianH
1,251 Expert 1GB
Except what mac11 said in regards to the parameter declaration will not work. The parameter should be:
Expand|Select|Wrap|Line Numbers
  1. float out[LATMAX60][LONMAX]
The first dimension shown (LATMAX60) is optional since in C/C++, this notation degrades into a pointer, so it is equivalent to:
Expand|Select|Wrap|Line Numbers
  1. float out[][LONMAX]
  2. // or
  3. float (*out)[LONMAX]
in a parameter list.

If you want to have type safety, you can use a reference to the array (in C++)
Expand|Select|Wrap|Line Numbers
  1. float (&out)[LATMAX60][LONMAX]
or in C, it would be a pointer to a 2d ARRAY:
Expand|Select|Wrap|Line Numbers
  1. float (*out)[LATMAX60][LONMAX]
but that would require that you dereference the array prior to you accessing the elements in it. Like this:
Expand|Select|Wrap|Line Numbers
  1. (*out)[row][col] = FMISSING;
The only other alternative is to do pointer arithmetic correctly. Like this:
Expand|Select|Wrap|Line Numbers
  1. *(out+row*latmax+col) = FMISSING;
  2.  
I think that is right. I usually let the compiler do the work for me.


Adrian
Jun 10 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Mike D. | last post by:
I have a problem with a dynamic library I am developing, but it is really more of a pointer issue than anything else. Hopefully someone here can lend me some assistance or insight into resolving...
11
by: Tatu Portin | last post by:
I have a struct: typedef struct { char **user_comments; int *comment_wds; int comments; char *vendor; } vorbis_comment;
6
by: Roman Mashak | last post by:
Hello, I belive the reason of problem is simple, but can't figure out. This is piece of code: struct timeval { long tv_sec; /* seconds */ long tv_usec; /* microseconds */ };
6
by: Kiran | last post by:
Hi all, What I am trying to do is to pass a pointer to the first element of an array to a function, modify it in that function, and then print out the values of the array (which has been modified...
2
by: thangviet | last post by:
Hey guys, I have a function which needs to read a file and use the pointer parameter given. the pointer is a structure with five members; int points, float *time, *dhdt, *drate and *diff. The .dat...
13
by: pereges | last post by:
Hi, can some one please tell me why this program is not able to function properly. I have a array a and i am trying to create a pointer array b which points to elements less than 40 in a. ...
1
by: Anvesh Tanuku | last post by:
fairly new C++ programmer, So in this program the first value i set for nu2 seems to be getting passed to the output the second one keeps outputting garbage. I am not sure if this has something...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...
3
SueHopson
by: SueHopson | last post by:
Hi All, I'm trying to create a single code (run off a button that calls the Private Sub) for our parts list report that will allow the user to filter by either/both PartVendor and PartType. On...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.