Skip to main content
gamedev.net
🔒 Locked

Converting from float[][] to float**

Started by Anfernius 5 replies 600+ views
Original Post
Anfernius
Anfernius
Hi, I would like to know how can i put the following code to work: float a[3][3]; float **b; b = a; // doesn''t work b = (float**) a; // Looses the pointer and i''m trying to do this without using malloc to initialize ''a''. Thanks for your atention.
niyaw
niyaw
there''s only one o in loses.

you can''t. a two-dimensional array of floats and an array of pointers to arrays of floats have different memory layouts and can''t be casted to one another.
CpMan
CpMan
Just going off the top of my head here.

float* g = &a[x]; sets pointer to first array element of a[x]



Gamedev for learning.
libGDN for putting it all together.
An opensource, cross platform, cross API game development library.
VSEDebug Visual Studio.NET Add-In. Enhances debugging in ways never thought possible.
fizban75
fizban75
float *b = &a[0][0];

This will give you a pointer to the first element in the array. Does that give you what you want?
cgoat
cgoat
float [2][3] is actually one contiguous block of 6 floats, so you can only cast it to a float*. Then you lookup a specific X,Y position like so:


  
const int width = 10;
const int height = 10;

float getindex(float* parray, int x, int y)
{
return parray[y*width + x];
}

float arraytest[width][height];
getindex(arraytest, 1, 1);

TerranFury
TerranFury
What about creating an array of float pointers, with each pointer pointing to the beginning of another column in the array, and passing that? (Yeah, it''s ugly. Yeah, you shouldn''t do it. But can you?)
niyaw
niyaw
yes you can.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.