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
5 2389
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 - int read3B4XRT2B (
-
int latmax,
-
FILE *fp,
-
float *out)
to something like this - int read3B4XRT2B (
-
float** latmax,
-
FILE *fp,
-
float *out)
and then change this -
*(*(out+row)+col) = FMISSING;
to something like - 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!
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: -
float (*ptr)[LONGMAX] = precip; /* OK */
-
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.
you must change - int read3B4XRT2B (
-
int latmax,
-
FILE *fp,
-
float *out)
to something like this - int read3B4XRT2B (
-
float** latmax,
-
FILE *fp,
-
float *out)
and then change this -
*(*(out+row)+col) = FMISSING;
to something like - 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
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
AdrianH 1,251
Recognized Expert Top Contributor
Except what mac11 said in regards to the parameter declaration will not work. The parameter should be: - 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: - float out[][LONMAX]
-
// or
-
float (*out)[LONMAX]
in a parameter list.
If you want to have type safety, you can use a reference to the array (in C++) - float (&out)[LATMAX60][LONMAX]
or in C, it would be a pointer to a 2d ARRAY: - float (*out)[LATMAX60][LONMAX]
but that would require that you dereference the array prior to you accessing the elements in it. Like this: - (*out)[row][col] = FMISSING;
The only other alternative is to do pointer arithmetic correctly. Like this: -
*(out+row*latmax+col) = FMISSING;
-
I think that is right. I usually let the compiler do the work for me.
Adrian
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
by: Tatu Portin |
last post by:
I have a struct:
typedef struct {
char **user_comments;
int *comment_wds;
int comments;
char *vendor;
} vorbis_comment;
|
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 */
};
|
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...
|
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...
|
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.
...
|
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...
|
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...
|
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...
|
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,...
|
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...
|
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...
|
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...
|
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,...
|
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...
|
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 ...
| |