473,779 Members | 2,016 Online
Bytes | Software Development & Data Engineering Community
+ 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)+co l) = 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 2409
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)+co l) = FMISSING;
else
*(*(out+row)+co l) = buffer[col] / SCALE;
}
}
return nread;
}

problem: this expression: *(*(out+row)+co l) = 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
5196
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 this. Ok... here goes.... I have a function that passes a pointer to a string to another function. For example: int FunctionA ()
11
2072
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
3030
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
7875
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 the function) in main. However, I am getting a segmentation fault. Here is the code: (Please note, the size is fixed in this code, but in my code where I am actually going to use this, the size of the array is not known until you get to the...
2
1595
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 file contains double values in three columns. I am required to open the .dat file, read the values and insert them into their appropriate members (1st column of data into *time, 2nd column into *dhdt and 3rd column into *drate). I have already...
13
1881
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. #include <stdio.h> #include <math.h> #include <stdlib.h> void create_ptr_list(int *a, int ***b, int n, int *size_ptr) {
1
1431
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 to do with the way I am passing my array to my function or whether this has something to do with me not dealing with the pointers the right way: #include<iostream> #include<math.h> using namespace std;
0
9474
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10138
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9930
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7485
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6724
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5373
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4037
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 we have to send another system
2
3632
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2869
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.