parameters pass between function and main 
Author Message
 parameters pass between function and main

Hi there

I am reading a big array from one data file
I define the array as
   float   **data;
   ...
   and read the array

/* ***************Reading in data*************** */
        fd_in = open(in_name,0);
        read(fd_in,header,256);parameters pass C
        for (i=0; i<no_tr_vec; ++i){
          if(read(fd_in,data[i],fsize*dim) != fsize*dim){
            printf(" Error reading data[%d]\n",i);
            fprintf(res_file," Error reading data[%d]\n",i);
            exit(0);
          }
        }
        n_read=close(fd_in);

  I have one function, for example if the array is 1200x900
  I'd like to have a loop to have the function process the array
   by data[1:100][1:900], data[101:200][1:900] etc.
   so how to define the parameter in the function?
   Thanks a lot!



Sat, 05 Nov 2005 05:18:46 GMT  
 parameters pass between function and main

Quote:

> Hi there

> I am reading a big array from one data file
> I define the array as
>    float   **data;
>    ...
>    and read the array

You've hit the wickedness of C multi-dimension arrays. In C89 there is no
neat way of declaing an array
  float[Nx][Ny]         where Nx and Ny are not known at compile time.

The best bet is to declare

float *data = malloc(Nx * Ny * sizeof(float));

Then do the index calculation manually. eg element[2][3]  = data[2 *Nx + 3];

The other way is to declare

float **data = malloc(Ny * sizeof(float *));
for(i=0;i<Ny;i++)
   data[i] = malloc(Nx * sizeof(float));

Now data is an array of pointers to float.

Quote:

> /* **************Reading in data*************** */

First point is that low-level open() read() etc are no longer part of
standard C. Use fopen(), fread() etc unless you have a really pressing need
not to.

Quote:
>         fd_in = open(in_name,0);
> read(fd_in,header,256);parameters pass C
>         for (i=0; i<no_tr_vec; ++i){

You can just read this with one call, if data is a single array. 2d arrays
are contiguous. However you can't if you opt for the second of my solutions
to the 2d problem.
Quote:
>           if(read(fd_in,data[i],fsize*dim) != fsize*dim){
>             printf(" Error reading data[%d]\n",i);
>             fprintf(res_file," Error reading data[%d]\n",i);
>             exit(0);
>           }
>         }
>         n_read=close(fd_in);



Sat, 05 Nov 2005 06:01:06 GMT  
 parameters pass between function and main
On Mon, 19 May 2003 23:01:06 +0100, "Malcolm"

        [snip]

Quote:
> > /* **************Reading in data*************** */
> First point is that low-level open() read() etc are no longer part of
> standard C. Use fopen(), fread() etc unless you have a really pressing need

I hope you mean:

"low-level open() read() etc are not, and have never been, a part of
standard C".

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq



Sat, 05 Nov 2005 11:31:55 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. How to pass parameters to main()

2. multithreading: passing parameter from the main to the thread

3. Passing C++ Class Member Function to as a C Callback Function Parameter

4. passing argc/argv into function from main

5. HELP: allocating arrays in function after passing ptr from main

6. pass command line arguments to main function

7. calling C++ function from Fortran: parameter passing

8. how do I pass arrays as parameters to functions - newbie

9. Passing parameters to a Thread'ed function

10. Pass parameter to a function

11. function passed as parameter....

12. Pb with passing a function as parameter

 

 
Powered by phpBB® Forum Software