473,466 Members | 1,366 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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

5 New Member
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 2389
mac11
256 Contributor
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 Recognized Expert Moderator Expert
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
mshaaban
5 New Member
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 Recognized Expert Top Contributor
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 Recognized Expert Top Contributor
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.