Passing a three dimensional array

Discussion in 'C Programming' started by Nitin, Mar 7, 2006.

  1. Nitin

    Nitin Guest

    Hi all

    How to pass a 3-D array from the main program and access it in the
    subroutine.

    for example

    void main()
    {
    .....
    float a_slope_n_river_width[250][200][5];
    ....
    /*read values from file*/
    ....
    fn_populate_slope_file ( v_x_pixels, v_y_pixels,
    a_slope_n_river_width, v_pixel_length);
    ...
    ...
    }

    void fn_populate_slope_file ( int v_x_pixels, int v_y_pixels, float
    a_slope_n_river_width[v_y_pixels][v_x_pixels][5], float v_pixel_length
    ) // Generate slope array
    {
    /* Here I want to access the element of three dimensional array */

    }

    This piece of code is not working ... Please let me know the error

    Thanks in advance

    Regards
    Nitin
    Nitin, Mar 7, 2006
    #1
    1. Advertising

  2. Nitin

    Xavier Guest

    Nitin wrote:
    > Hi all
    >
    > How to pass a 3-D array from the main program and access it in the
    > subroutine.
    >
    > for example
    >
    > void main()
    > {
    > .....
    > float a_slope_n_river_width[250][200][5];
    > ....
    > /*read values from file*/
    > ....
    > fn_populate_slope_file ( v_x_pixels, v_y_pixels,
    > a_slope_n_river_width, v_pixel_length);

    may be your v_x_pixels, v_y_pixels are not initialized with values <= dim
    > ...
    > ...
    > }
    >
    > void fn_populate_slope_file ( int v_x_pixels, int v_y_pixels, float
    > a_slope_n_river_width[v_y_pixels][v_x_pixels][5], float v_pixel_length
    > ) // Generate slope array
    > {
    > /* Here I want to access the element of three dimensional array */
    >
    > }
    >
    > This piece of code is not working ... Please let me know the error
    >
    > Thanks in advance
    >
    > Regards
    > Nitin
    >


    if your compiler accepts C99 it may be ok...

    I had juste one trouble with your code : your array is too large...
    (segmentation fault occurs ... and disapear With small v_x_pixels, v_y_pixels)

    What is YOUR trouble?


    Xavier
    Xavier, Mar 7, 2006
    #2
    1. Advertising


  3. > void fn_populate_slope_file ( int v_x_pixels, int v_y_pixels, float
    > a_slope_n_river_width[v_y_pixels][v_x_pixels][5], float v_pixel_length


    I am not sure if the compiler you are using implements C99 or not but
    you can try

    void n_populate_slope_file (int v_x_pixels, int v_y_pixels, float***
    a_slope_n_river_width, float v_pixel_length )

    and use the values v_x_pixels, v_y_pixels and v_pixel_length to access
    the 3D Array
    using pointer.
    Prashant Mahajan, Mar 8, 2006
    #3
  4. "Prashant Mahajan" <> writes:
    >> void fn_populate_slope_file ( int v_x_pixels, int v_y_pixels, float
    >> a_slope_n_river_width[v_y_pixels][v_x_pixels][5], float v_pixel_length

    >
    > I am not sure if the compiler you are using implements C99 or not but
    > you can try
    >
    > void n_populate_slope_file (int v_x_pixels, int v_y_pixels, float***
    > a_slope_n_river_width, float v_pixel_length )
    >
    > and use the values v_x_pixels, v_y_pixels and v_pixel_length to access
    > the 3D Array
    > using pointer.


    Please don't snip attribution lines (the lines that say
    "So-and-so" <> writes:).

    There's nothing C99-specific in your declaration of n_populate_slope_file.
    C99 does add some features in the area of array-like parameters,
    but you don't use any of them.

    And keep carefully in mind that a pointer-to-pointer-to-pointer is not
    a way to access the elements of a true 3-dimensional array (i.e., an
    array of arrays of arrays). See section 6 of the comp.lang.c FAQ.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, Mar 8, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alf P. Steinbach
    Replies:
    0
    Views:
    431
    Alf P. Steinbach
    Aug 18, 2003
  2. John Harrison
    Replies:
    4
    Views:
    6,921
    Default User
    Aug 19, 2003
  3. Icosahedron
    Replies:
    8
    Views:
    650
    Vivek
    Aug 21, 2003
  4. Venkat
    Replies:
    4
    Views:
    969
    Venkat
    Dec 5, 2003
  5. Wirianto Djunaidi
    Replies:
    2
    Views:
    200
    Wirianto Djunaidi
    Apr 29, 2008
Loading...

Share This Page