I have an array of pointers
float** data = new float*[NX];
for(int i = 0; i < NX; ++i)
{
data[i] = new float[NY];
}
I decleared multiarray like that, then i put some values in it this way (and confirmed that values are correct with cout -
Loop{
data[i][j] = someValue;
cout << data[i][j];
}
I have quite complex values in there like 10663.3, 11140.6 etc which need to be in the right position, so i dont think i could be getting false positives in there
Finally im trying to write the data into NetCDF file
The method looks like that
NcBool put(const float* vals, long c0, long c1, long c2, long c3, long c4)
Taken from this example: http://www.unidata.ucar.edu/software/netcdf/examples/programs/simple_xy_wr.cpp
I did it like that
fileData->put(&data[0][0], NX, NY);
However, when i start reading from the file i get gibberish. My guess is that i am giving the array to the method in a wrong way. However i cannot figure out the right way.
I would also appreciate a good tutorial for Pointers to pointers. I have not been able to find one
When you create an array of arrays the way you are doing, it is not contiguous. You basically have NX
arrays in NX
different locations somewhere in memory. I don't know what the put
function does, but based on its signature, it probably expects a contiguous array of floats. But whatever it expects, it can't possibly have access to your data, other than the first array, because the information about the location of those other arrays is not available from &data[0][0]
.
What you could do instead is create a single dimensional array, and treat it as two dimensional with some simple math.
float * data = new float[NX * NY];
This is how you would access the array at position (x,y):
data[x * NY + y] = 1.234;
Then pass it to the put
function like this:
fileData->put(data, NX, NY);
You probably would want to encapsulate this in a class that handles the arithmetic for you, or you could use a library where that has already been done, such as Boost.MultiArray.