Read Multidimensional Array Constructor

I am writing a class FireWeatherWRF and i have a private declaration data_array[264][345] that is called and initialised in my constructors. After I manipulate the data, I want to be able to access specific elements. So I created a constant member function getArray(). But I still can't get it to work. The specific error messages are in bold

1
2
3
4
5
6
7
8
9
10
11
12
13
14
//function declaration
double **getArray();

//function
double ** FireWeatherWRF::getArray(){
  double **data_array_ptr = data_array; //error: cannot convert ‘double (*)[345]’ to ‘double**’ in initialization 
  return data_array_ptr;
}

//function call
double array[264][345];
array = **(latitude.getArray());//error: incompatible types in assignment of ‘double’ to ‘double [264][345]’ 
cout<<array[0][0]<<endl;
Last edited on
given a 2D array such as int data_array[5][8], then data_array is

int [5][8] // [5][8] array of int

NOT **int //pointer to pointer to int

So:
1
2
3
4
5
6
7
8
9
10
11
12
int twoD_Array[4][8]; //two dimensional array

int main()
{

    int (*Ptr_2D_Array) [4][8] =  &twoD_Array; //Note the use of the  & address operator on the array  
    //Use the pointer

    (*Ptr_2D_Array)[3][2] = 99;//EDIT - added brakets for clarification
    
   return 0;
}


If you want a funtion returning a pointer to the 2D array
1
2
3
4
5
6
7
8
//A function returning a pointer to a 2D array of int

int (* FunctionName()) [4][8]
{

    int (*Ptr_2D_Array) [4][8] =  &twoD_Array; //Note the use of the  & address operator on the array
    return Ptr_2D_Array;
}
Last edited on
But aren't arrays stored as just addresses in memory? Also, a 2d array needs two **, the syntax is really the part I am having the most difficulty with-
stavros wrote:
Also, a 2d array needs two **


if you have a prper array like int array_name [7][9] then:

1
2
int **p = array_name //error
int **p = &array_name; //error 


Maybe you are getting confused with the dynamically created fake 2D arrays using new like:

1
2
3
4
5
6
7
8
   int ** pppp = new int*[7];

   for (int count =0; count < 7; ++count)
    {
        pppp[count] = new int [9];
    }

    pppp[3][6] = 24;
Hey tash,

You might want to rethink the interface to your class.

The idea behind object orientation is that the class is a "thing" that has certain "behaviors" and "state". Ideally, you create an interface to the class that allows performing any of the class's actions without touching its state. "getters", like getArray() above, are a sign that you might be exposing too much of the class's internals to the world (versus making various operations available through its public member functions).

If you insist on being able to get data out of the class, I'd rewrite getArray() to allow retrieval of a single element.

Returning a pointer to the array from the class is a Bad Idea. This means external actors can change the internal state of your object, which is really asking for trouble. If you really, really need the array outside the class (and God help you, if you decide to go this route), you should create a separate copy of it using memcpy(), and pass a pointer to the COPY of the array outside your object. But then you have to make sure to free() the array later, otherwise you're going to leak memory. Keeping track of all this is ugly and complicated.

The easy, simple, and correct thing to do is to rewrite your class's interface in such a way that the class itself performs any operations requiring access to the array -- don't try to pass the array out of the class.
array 264 345
is it not too big? try smaler number
getArray() is really just a check for me. When I produce the code for others, I won't allow them to getArray(), but right now- I want to see that I am doing the calculations to each element as I manipulate it- don't worry! I am not going to allow the user to make changes to the array once the other functions work the way I want them to work.

I am not sure that the size is the problem- syntax is. Unless anyone else knows a better way for me to check the array- I suppose I could write a function that allows me to see one element at a time.
give me the full code
so we can fix it together
that's the only part of the code you need- if you personally know me (posted a link to this forum ? on my fb) email me and we can talk.
For testing, just make the array public and access it directly from outside the class.

Btw, qabil is right, 264x345 really is too big, you should probably use dynamic memory for this. I'll talk to you more when I get back. - DA
Thanks all for your help. I worked with a different language when handling MUCH larger arrays & wasn't aware that that size array was too big. For any one reading this forum I ended up making a pointer to an array containing points to arrays. (see syntax below) & then making it public to check, but will keep it private when the code is done.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

#include <iostream>
using namespace std;

typedef int* IntArrayPtr;

int d1=3, d2=4;                                                     // array dimensions

IntArrayPtr *m=new IntArrayPtr[d1];
for (int i=0;i<d1;i++) { m[i]=new int[d2];}           // m is now d1xd2 array

for (i=0;i<d1;i++){
    for (int j=0;j<d2;j++){
         m[i][j]=i+j;
    }
}                                                                           // filling array elements

for (i=0;i<d1;i++){ delete [] m[i];}
delete [] m;                                                             //releasing array from freestore

Topic archived. No new replies allowed.