Jump to content
  • Advertisement
Sign in to follow this  
JDUK

Simple(i hope) pointer/2D array problem

This topic is 4595 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

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
Advertisement
I don't see why you would need this line ...
int (*lp)[Brightness];
I think you can just declare it as
int *lp;

Share this post


Link to post
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).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

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

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!