pointer arrrays
i want to make a pointer to and array like this [size][3] in c++. size is a number loaded from a file. but i don''t know how cause i tried a few way but it didn''t work. anyone know how?
Easiest is to do the multiply yourself:
float * f = new float[ size*3 ];
float0fromnth = f[ n*3 ];
float1fromnth = f[ n*3+1 ];
float2fromnth = f[ n*3+2 ];
If you really want the array like that, that should work fine, as the "last" sizer of the array can be dynamic, but the others cannot.
float * f = new float[size][3]; // works
float * f = new float[3][size]; // doesn''t work
Here''s some code:
float (*f)[3];
void func( int size )
{
f = new float[size][3];
}
Remember that declarations read from in to out, first right, and then left (paying attention to parens).
Thus:
float *f[3]; // f is array of 3 elements of pointer to float
float (*f)[3]; // f is pointer to array of 3 elements of float
float * f = new float[ size*3 ];
float0fromnth = f[ n*3 ];
float1fromnth = f[ n*3+1 ];
float2fromnth = f[ n*3+2 ];
If you really want the array like that, that should work fine, as the "last" sizer of the array can be dynamic, but the others cannot.
float * f = new float[size][3]; // works
float * f = new float[3][size]; // doesn''t work
Here''s some code:
float (*f)[3];
void func( int size )
{
f = new float[size][3];
}
Remember that declarations read from in to out, first right, and then left (paying attention to parens).
Thus:
float *f[3]; // f is array of 3 elements of pointer to float
float (*f)[3]; // f is pointer to array of 3 elements of float
so if i did this
float (*f)[3];
f = new float[size][3];
will it make and array of size size and each size has 3 floats?
float (*f)[3];
f = new float[size][3];
will it make and array of size size and each size has 3 floats?
Oh, btw, a more C++-ish way (yeah I know new is kinda C++ already) :
vector<vector<float> > v;v.resize( size, vector( 3 ) );// and if you have some boost or other lib with stackvector<stack<3,float> > v;v.resize( size );
I believe that what you are trying to do is dynamically allocated a two-dimensional array:
This will create a two-dimensional array the same size (dimension) of this static 2D array:
For an explanation on how this works, have a look on the GDNet forum search or on Google.
[ Google || Start Here || ACCU || MSDN || STL || GameCoding || BarrysWorld || E-Mail Me ]
[edited by - Lektrix on June 19, 2003 4:27:46 PM]
int Height = ... ;int Width = ... ; // allocationfloat** array2D = new float*[Height];for (int i = 0; i < Height; ++i) array2D[ i ] = new float[Width]; // use, e.g.array2D[2][3] = 5.6farray2D[5][9] = 1.3farray2D[0][0] = 3.14f // deallocation (free)for (int i = 0; i < Height; ++i) delete [] array2D[ i ];delete [] array2D;
This will create a two-dimensional array the same size (dimension) of this static 2D array:
float staticArray2d[Height][Width];
For an explanation on how this works, have a look on the GDNet forum search or on Google.
[ Google || Start Here || ACCU || MSDN || STL || GameCoding || BarrysWorld || E-Mail Me ]
[edited by - Lektrix on June 19, 2003 4:27:46 PM]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement