Passing a three dimensional array

N

Nitin

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
 
X

Xavier

Nitin said:
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
 
P

Prashant Mahajan

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.
 
K

Keith Thompson

Prashant Mahajan said:
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

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top