Correct return/reference for Multidimensional Structure

Hi there,
I have 2 structures defined as follows:
1
2
3
4
5
6
7
8
9
10
struct afb2d_Adata{
int hpfR,hpfC,lpfC,lpfR;
float *hpf;
float *lpf;
}

struct afb2d_data{
afb2d_Adata LLLH;
afb2d_Adata HLHH;
}


I want to create a multidimensional afb2d_data struct as follows:
afb2d_data w[J][2][2];
and pass it to a function either by reference or to get it back as a return. This is where I am having trouble. I have tried the following:
1
2
3
4
5
6
7
8
9
10
11
void rootfunc()
{
int J=2; //this is for illustration purposes, this changes based on user inp
afb2d_data w[J][2][2];
func1(&w); 
}

func1(afb2d_data *w[][2][2])
{ 
//init & data processing
}

OR the return way:
1
2
3
4
5
6
7
afb2d_data*** func1()
{
int J=2;
afb2d_data w[J][2][2];
//init & data processing
return w;
}


However neither work. Could someone help me figure out the correct method to do this (either return or pass by reference is ok with me). Thanks for the time!
You cannot declare a dynamic array this way. You must use the new operator to allocate the memory dynamically.

1
2
3
4
5
6
7
8
9
afb2d_Adata ***my3DArray = new afb2d_Adata**[J];
for (int i = 0; i < J; i++)
{
    my3DArray[i] = new afb2d_Adata*[2];
    for (int j = 0; j < 2; j++)
    {
        my3DArray[i][j] = new afb2d_Adata[2];
    }
}


NOTE: Most people avoid multi-dimensional arrays, and I'm one of those people. Most of the time you can do without them.
Last edited on
Hi webJose,
Thanks for your help again! You are a lifesaver today. I have to use multidimensional arrays in this system because of the nature of my coefficient decomposition. Now to pass this array to another func I can do the following right? :
1
2
3
4
void rootfunc(){
//init
func1(&my3DArray)
}


and de-reference as so:
1
2
3
4
func1(afb2d_Adata ****w)
{
//do processing on w
}

Now can I still access the array withing func1 as say w[0][0][0] ?
Thanks!
Yes.
Thank you!
Topic archived. No new replies allowed.