Pointers list going wrong

94 Views Asked by At

I want to be able to access the arrays and variables with pointers. So I made an array of pointers to those arrays and variables. But it doesnt seem to compile right. Where do I go wrong? (c++, atmel, avrstudio)

volatile uint8_t    lfo1Clock = 0;
volatile uint8_t    lfo2Clock = 0;
volatile uint8_t    lfo3Clock = 0;
volatile uint8_t    lfo4Clock = 0;
volatile uint8_t    lfo5Clock = 0;
volatile uint8_t    lfo6Clock = 0;

uint8_t     lfo1[5]={4,0,0,0,0};
uint8_t     lfo2[5]={4,0,0,0,0};
uint8_t     lfo3[5]={4,0,0,0,0};
uint8_t     lfo4[5]={4,0,0,0,0};
uint8_t     lfo5[5]={4,0,0,0,0};
uint8_t     lfo6[5]={4,0,0,0,0};

uint16_t    *lfoList [] = {lfo1,lfo2,lfo3,lfo4,lfo5,lfo6};
uint16_t    *lfoClockList [] = {lfo1Clock,lfo2Clock,lfo3Clock,lfo4Clock,lfo5Clock,lfo6Clock};

Thanks for any advice.

2

There are 2 best solutions below

1
On
volatile int    lfo1Clock = 0;

int lfo1[5]={4,0,0,0,0};

int (*lfoList[5])[] = {&lfo1}; //   array of pointers to array of five integers

volatile int *lfoClockList [] = {&lfo1Clock}; 

Note: You are using uint16_t* pointer to infer uint8_t data

2
On

Your first array is an array of uint16_t pointers, but it's pointing to uint8_t arrays. It should be something like this:

uint8_t    *lfoList [] = {lfo1,lfo2,lfo3,lfo4,lfo5,lfo6};

Your second array is also an array of pointers of the wrong type, but it also contains simple uint8_t instances which wouldn't work even if the type was correct.

If it's just meant to be an array of uint8_t instances, I suspect you want this:

uint8_t    lfoClockList [] = {lfo1Clock,lfo2Clock,lfo3Clock,lfo4Clock,lfo5Clock,lfo6Clock};

If you actually did need the second array to be an array of uint8_t pointers, then you'll need to take the address of each of those items. And I think you need to use volatile in the array definition for the type to be compatible.

volatile uint8_t    *lfoClockList [] = {&lfo1Clock,&lfo2Clock,&lfo3Clock,&lfo4Clock,&lfo5Clock,&lfo6Clock};