Go Down

Topic: a pointer to a multidimensional array (Read 1 time) previous topic - next topic

kasperkamperman.com

I'm trying to make a pointer to a multidimensional array. Any idea how to do that?

In my old post I already found out how to do that with one array.
http://arduino.cc/forum/index.php/topic,53807.0.html

My code.
Code: [Select]

int _rybWheel[25][3] =
{ { 0  , 255, 255 }, // 0
{ 12 , 255, 255 }, // 15
{ 24 , 255, 255 }, // 30
{ 30 , 255, 255 }, // 45
{ 36 , 255, 255 }, // 60
{ 42 , 255, 255 }, // 75
{ 48 , 255, 255 }, // 90
{ 54 , 255, 255 }, // 105
{ 60 , 255, 255 }, // 120
{ 72 , 255, 255 }, // 135
{ 84 , 255, 255 }, // 150
{ 108, 255, 255 }, // 165
{ 120, 255, 255 }, // 180
{ 154, 255, 255 }, // 195
{ 180, 255, 255 }, // 210
{ 206, 255, 255 }, // 225
{ 225, 255, 255 }, // 240
{ 240, 255, 255 }, // 255
{ 260, 255, 255 }, // 270
{ 265, 255, 255 }, // 285
{ 280, 255, 255 }, // 300
{ 300, 255, 255 }, // 315
{ 315, 255, 255 }, // 330
{ 333, 255, 255 }, // 345
{ 360, 255, 255 }  // 360 
};


I declare the array in the .h file like this:
Code: [Select]

int* rybWheel;


And in the constructor I write.
Code: [Select]

rybWheel = _rybWheel;


And I do this in a function (thats the error on line 360)
Code: [Select]
Serial.println(rybWheel[rybWheelIndex+1][0],DEC);

I get these errors:
Code: [Select]

/Users/macbook/Dropbox/code/_arduino/libraries/ColorLight/ColorLight.cpp: In constructor 'ColourPalette::ColourPalette()':
/Users/macbook/Dropbox/code/_arduino/libraries/ColorLight/ColorLight.cpp:55: error: cannot convert 'int [25][3]' to 'int*' in assignment
/Users/macbook/Dropbox/code/_arduino/libraries/ColorLight/ColorLight.cpp: In member function 'void ColourPalette::ColourAtRYBWheel(int, int*)':
/Users/macbook/Dropbox/code/_arduino/libraries/ColorLight/ColorLight.cpp:360: error: invalid types 'int[int]' for array subscript


I've looked up some information on pointers, but I cannot find clear information about pointers to multidimensional arrays and how I can use that with Arduino.

PaulS

A two dimensional array is really an array of pointers to one dimensional arrays. So, a pointer to a 2D array needs two stars.
Code: [Select]
int** rybWheel;


kasperkamperman.com

Yes, but if I try that then I get this error:

Code: [Select]

/Users/macbook/Dropbox/code/_arduino/libraries/ColorLight/ColorLight.cpp:55: error: cannot convert 'int [25][3]' to 'int**' in assignment


I tried this as well:
Code: [Select]

rybWheel = &_rybWheel;

error: /Users/macbook/Dropbox/code/_arduino/libraries/ColorLight/ColorLight.cpp:55: error: cannot convert 'int (*)[25][3]' to 'int**' in assignment


Pointer is still a hard subject to grab for me.

PaulS

Quote
Yes, but if I try that then I get this error:

Lean a little to the left. I can't see line 55.

A little further...

No, still can't see.

Ooh, careful, don't fall off the chair.

kasperkamperman.com

Ok, sorry for not clearly documenting it. I've made a minimized version of the code, that gives me the error.

The Colorlight.h file
Code: [Select]

#ifndef ColorLight_h
#define ColorLight_h

#include "WProgram.h"

class ColourPalette
{
  public:
    ColourPalette();
   
    int** rybWheel;
};
#endif


The ColorLight.cpp file
Code: [Select]

#include "WProgram.h"
#include "ColorLight.h"

int _rybWheel[25][3] =
{ { 0  , 255, 255 }, // 0
{ 12 , 255, 255 }, // 15
{ 24 , 255, 255 }, // 30
{ 30 , 255, 255 }, // 45
{ 36 , 255, 255 }, // 60
{ 42 , 255, 255 }, // 75
{ 48 , 255, 255 }, // 90
{ 54 , 255, 255 }, // 105
{ 60 , 255, 255 }, // 120
{ 72 , 255, 255 }, // 135
{ 84 , 255, 255 }, // 150
{ 108, 255, 255 }, // 165
{ 120, 255, 255 }, // 180
{ 154, 255, 255 }, // 195
{ 180, 255, 255 }, // 210
{ 206, 255, 255 }, // 225
{ 225, 255, 255 }, // 240
{ 240, 255, 255 }, // 255
{ 260, 255, 255 }, // 270
{ 265, 255, 255 }, // 285
{ 280, 255, 255 }, // 300
{ 300, 255, 255 }, // 315
{ 315, 255, 255 }, // 330
{ 333, 255, 255 }, // 345
{ 360, 255, 255 }  // 360 
};


ColourPalette::ColourPalette()
{
rybWheel = _rybWheel;
}


This will give the error below:
Code: [Select]

/Users/macbook/Dropbox/code/_arduino/libraries/ColorLight/ColorLight.cpp: In constructor 'ColourPalette::ColourPalette()':
/Users/macbook/Dropbox/code/_arduino/libraries/ColorLight/ColorLight.cpp:37: error: cannot convert 'int [25][3]' to 'int**' in assignment


Line 37 is
Code: [Select]
rybWheel = _rybWheel;

Go Up