Sign in to follow this  
JDUK

Simple(i hope) pointer/2D array problem

Recommended Posts

JDUK    100
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 this post


Link to post
Share on other sites
foo-bar-    122
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).

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this