Array memory slot definition errors

Hello,

just a quick probably easy to asnwer question

I have set my code like that:

byte CellCount = 3;

byte BatteryPins[CellCount] = {A0, A1, A2};

and when I put the variable "CellCount" in the array memory definer it simply wont accept it how should I do it?

Thanks for helping.

What version of the IDE are you using and what board?

const byte CellCount = 3;

byte BatteryPins[CellCount] = {A0, A1, A2};

(See what I did with code tags there ?)

You need to do it like this

const byte CellCount = 3;

The compiler won't accept a variable for an array size definition

...R

Thanks guys that was helpful just tried that new thing...

Robin2:
You need to do it like this

const byte CellCount = 3;

The compiler won't accept a variable for an array size definition

...R

Can't you just leave it blank?

INTP:
Can't you just leave it blank?

Leave what blank?

...R

I think this is more flexible

byte BatteryPins[] = { A0, A1, A2 };

const byte CellCount = sizeof(BatteryPins);

Whandall:
I think this is more flexible

byte BatteryPins[] = { A0, A1, A2 };

const byte CellCount = sizeof(BatteryPins);

I can see where you are coming from but I think that will only work if the array is an array of bytes.

Also, I think the choice between your approach and that in the Original Post must take account of the purpose of the array. If the purpose is to set aside a specified amount of space (but not necessarily to fill it immediately) the style in the Original Post is more suitable.

...R

Robin2:
I can see where you are coming from but I think that will only work if the array is an array of bytes.

Which the given array is, so what?

Robin2:
in the Original Post must take account of the purpose of the array.

Which can only be guessed by the usage of A0 etc, besides that it was only a snippet that was given,
it is not sensible to assume anything IMHO.

byte BatteryPins[] = { A0, A1, A2 };

const byte CellCount = sizeof(BatteryPins) / sizeof (BatteryPins [0]);

...is better still, IMO.
It teaches good habits and saves tears and prevents hair loss later.

Translates to 'size of all total elements divided by size of one element'

Side question- does the Arduino lib already have defines for A0-A5 = 14-19?

Whandall:
Which the given array is, so what?

I was just making the point that it is not a general solution.

in the Original Post must take account of the purpose of the array.

Which can only be guessed by the usage of A0 etc, besides that it was only a snippet that was given,
it is not sensible to assume anything IMHO.

That was intended to be a general comment, not specifically related to the OP's problem.

I agree 100% that your solution is good for the OP's specific case.

...R

does the Arduino lib already have defines for A0-A5 = 14-19?

Try printing A0, A1 etc.

What you get depends on what board you are using.

INTP:
Translates to 'number of total elements divided by size of one element'

Not quite. It's the size of the total elements divided by the size of one element.