c++ - Get An Row From Dynamically Allocated 2D Array Pointer -


i'm pretty novice c++ , have been trouble working way through pointers.

//define colormaps (for brevity null) //256 rgb combinations in each colormap uint8_t colormap0[256][3]; uint8_t colormap1[256][3]; //insert color maps in storage array via pointer uint8_t *colormaps[] = {*colormap0, *colormap1};  //well define current map index read colormaps uint8_t colormapindex = 0; //we define pointer active array we'll update in loop() uint8_t *currentcolormap; 

occasionally we'll reassign current color map

currentcolormap = colormaps[colormapindex]; 

and other times values it

uint32_t c = getcolorfrommap(125); 

we'll need these functions well

// create 24 bit color value b,g,r uint32_t color(uint8_t r, uint8_t g, uint8_t b) {   uint32_t c;   c = r;   c <<= 8;   c |= g;   c <<= 8;   c |= b;   return c; }  uint32_t getcolorfrommap(byte indexvalue) {   uint8_t rgb = currentcolormap[indexvalue];   return color(rgb[0], rgb[1], rgb[2]); } 

the trouble lies in how values current color map

the current code giving me 3'invalid types 'uint8_t {aka unsigned char}[int]' array subscript' errors in return color(rgb[0],....

i've tried pointers currentcolormap get:

invalid type argument of unary '*' (have 'uint8_t {aka unsigned char}') 

i have use soultion low memory foot print going on arduino. making 256x3 byte array killing dynamic memory.... compiled. if not 1 thing other!

in c/c++, multi-dimensional array isn't set of 2 arrays in other languages. rather, int arr[a][b] same int arr[a*b] except math behind scenes. if think it, makes since mathematically. allows write int arr[x][y] following operation:

int arr[x*b+y] 

think way: have each x row after 1 another, each populated ys. if go length of x row can access specific entry. in example, rgb equal indexvalueth color byte, not full color.

however, there problem. have pointer currentcolormap array want use. when copying multi-dimensional array pointer, compiler "forgets" used be. you, have simple work it.

uint8_t *rgb = currentcolormap + indexvalue * 3; 

what doing here? we're taking pointer of currentcolormap , adding three times index. why 3 times? because second dimension three, or, because there 3 bytes per color. color pointer real color. can treat did before:

return color(rgb[0], rgb[1], rgb[2]); 

Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -