Public Group

Simple(i hope) pointer/2D array problem

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

Recommended Posts

My problem is I have 3 arrays that reprisent the spread of light from a light source. Lights1[3][3] Lights1[5][5] Lights1[7][7] and i want to read through my lightmap, when i find a light i want to read through the applicable Lights array and draw the llighting effect to my tiles. Here is the code that works out which Lights array i need to start reeding.
int Brightness = Lights[x][y]; // Gets value form the lightmap, either 3,5, or 7
int (*lp)[Brightness];// Makes pointer to point at elements of the correct array
switch(Brightness)
{
case 3: lp = Lights1; //point at the 1st element of the [3][3] array with a int[3] pointer
break;
case 5: lp = Lights2;//point at the 1st element of the [5][5] array with a int[5] pointer
break;
case 7: lp = Lights3; //point at the 1st element of the [7][7] array with a int[7] pointer
break;
}


I get that it doesnt like me declaring the pointer with with Brightness... but why an how should i resolve this. here are the compiler errors:
cannot convert int[3][3]' to int (*)[((unsigned int)((int)Brightness))]' in assignment
cannot convert int[5][5]' to int (*)[((unsigned int)((int)Brightness))]' in assignment
cannot convert int[7][7]' to int (*)[((unsigned int)((int)Brightness))]' in assignment

I'm sure im missing somthing obvious... Thanks.

Share on other sites
I don't see why you would need this line ...
int (*lp)[Brightness];
I think you can just declare it as
int *lp;

Share on other sites
you can always reserve the space using malloc:

int *lp = (int*)malloc(sizeof(int) * Brightness);

and then you can access lp as an array (using the index operators).

• What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

(You must login to your GameDev.net account.)

• 14
• 11
• 28
• 15
• 39
• Forum Statistics

• Total Topics
634835
• Total Posts
3019539
×