By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,655 Members | 1,005 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,655 IT Pros & Developers. It's quick & easy.

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

P: 5
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
Share this Question
Share on Google+
5 Replies


100+
P: 256
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
Expert Mod 5K+
P: 9,197
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

P: 5
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
Expert 100+
P: 1,251
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
Expert 100+
P: 1,251
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

Post your reply

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