Go Down

Topic: Embarrassing Array Question (Read 921 times) previous topic - next topic

Melbfella

G'day all,

I'm embarrassed to have to ask such an elementary question on this forum, but I can't find an answer that suits what I'm trying to do.

I have 2 arrays defined like this :

Code: [Select]

byte displayDigitsBank1[] = {char_space,char_space,char_space,char_space,char_space,char_space,char_space,char_space,char_space};
byte displayDigitsBank2[] = {char_space,char_space,char_space,char_space,char_space,char_space,char_space,char_space,char_space};



From there, based on which bank is selected, I'm trying to assign one of those arrays to a new array variable, like this :

Code: [Select]

if(bankNumber==1)
    {
       byte displayDigits[]=displayDigitsBank1[];
    }
///...else if bankNumber==2 etc etc


Any help to get me past this would be most appreciated.

Cheers...

Delta_G

You want to copy the entire array?  Why not just make the digitsBank point to digitsBank1 and skip the copying step?
|| | ||| | || | ||  ~Woodstock

Please do not PM with technical questions or comments.  Keep Arduino stuff out on the boards where it belongs.

Melbfella


You want to copy the entire array?  Why not just make the digitsBank point to digitsBank1 and skip the copying step?


Thanks for your quick replay Delta - that sounds like just what I'm after. Could you show me an example of how to do that?

Kai_Heinz

Hi,
in C you can't copy entire arrays in a single step. You have to use a loop instead and copy every singly entry. The better idea is to use a pointer and just point to the array you want to display:
Code: [Select]

byte displayDigitsBank1[] = {char_space,char_space,char_space,char_space,char_space,char_space,char_space,char_space,char_space};
byte displayDigitsBank2[] = {char_space,char_space,char_space,char_space,char_space,char_space,char_space,char_space,char_space};
byte *displayDigits; // <--- The Pointer

if(bankNumber == 1)
    displayDigits = &displayDigitsBank1[0];
else if(bankNumber == 2)
    displayDigits = &displayDigitsBank2[0];

Afterwards you can use the pointer just like an array:
Code: [Select]

byte exampleByte = displayDigits[0];

AWOL

Quote
in C you can't copy entire arrays in a single step.

True, but if you wrap them in a structure, magic happens.
Not that the OP needs this.

Melbfella

Thanks all, problem resolved, using the pointer.


Go Up