Jump to content
  • Advertisement


This topic is now archived and is closed to further replies.


pointer arrrays

This topic is 5654 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post

Link to post
Share on other sites
Guest Anonymous Poster
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).


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

Share this post

Link to post
Share on other sites
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 stack

vector<stack<3,float> > v;
v.resize( size );

Share this post

Link to post
Share on other sites
I believe that what you are trying to do is dynamically allocated a two-dimensional array:
int Height = ... ;
int Width = ... ;

// allocation

float** array2D = new float*[Height];
for (int i = 0; i < Height; ++i)
array2D[ i ] = new float[Width];

// use, e.g.

array2D[2][3] = 5.6f
array2D[5][9] = 1.3f
array2D[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]

Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!