Variable Types as Applied to Arrays

First of all, thanks for your patience since this may be a really noobish question.

I have a couple of arrays with a maximum of 5 values between the two of them. FWIW These values actually relate to digital pins 9 - 13. The array values are selected by user input and can be reset during operation.

As it stands now I fill these two arrays with zeroes whenever the values are to be redefined since more or less positions may be filled the next time around. Elsewhere in the sketch these arrays are used to indicate which pins should be set HIGH or LOW based on other “user-set” timing variables.

Right now, I just count through all the array positions with a for-loop no matter how many “real” (non-zero) values are in there. Essentially the sketch will set pin 0 HIGH (or LOW) when the count reaches positions in the array beyond the values the user intends.

Is there any “real” action taken when the chip is told to set pin zero HIGH or LOW? Should I append a special value to indicate the array is over? Should I check that the value extracted from the array is > zero? Is there some other accepted method of counting through an array of unknown length?

I just don’t have the experience to know if this extra effort is even necessary.

Right now I am declaring these arrays as int syncdArray[4] and int oppArray[4].

Am I correct in thinking that I could instead use byte instead of int when declaring the arrays since all the values are positive and between 0-255?

Thanks so much for your help and input.


PS If I call function loop(); to get out of an option selection menu, is that okay? It seems to work.

As long as you can tell whether the entry is “valid” (9-13) or “invalid” (0), it sounds fine to me. If pin 0 were a possible valid setting, I’d use -1 for the “invalid” marker. The overhead of checking a few extra array entries is miniscule; unless you make a giant array I wouldn’t worry about it.

I have a couple of arrays with a maximum of 5 values between the two of them.

If this means you actually need an array with 5 indexes you'll need to declare them as int array[5]; :)

Halley, Thanks for your response. Am I to understand that there is no effect by setting pin 0 HIGH or LOW?

AlphaBeta, Thanks too for your response. Isn't the extra position only required when it is a "char array"? Since arrays are zero indexed I should declare an "int array" with 5 positions as:int array [4]; Whereas if I needed a "char array" with 5 positions I should declare it as:char array [5]; Please correct me if I'm wrong.

My other question was if it is okay to use byte in lieu of int when declaring my small arrays. My hunch is yes I should but I suppose I should just try it and find out empirically! ;)

Thanks again, Sam

If you want an int array with 5 locations, you must declare:

int myarray[5];

You can then use the locations numbered 0 to 4:

myarray[0] = 10;
myarray[1] = 20;
myarray[2] = 30;
myarray[3] = 40;
myarray[4] = 50;

Arrays are declared just the same whether character or integer. Character arrays that are going to be used as nul-terminated strings require an extra character to be declared. All array index numbers begin at zero.

The number of indices in your array is independent of the size of each individual index. That is to say if you declare

int myArray[4];

you can change that to

char myArray[4];

Both of these have 4 places allocated. However, the int array is double the size of the char array. You access them the same way tho.

myValue = myArray[1];

This will give you the second value of each of those arrays. The data however, will either be int or char depending on your declaration.

However, the char array is double the size of the int array.

I think wanted to say that the other way around ;)

I think wanted to say that the other way around

Oops, you are correct.


Thanks to all for a thorough explanation!

Sam ;)

Am I to understand that there is no effect by setting pin 0 HIGH or LOW?

No, pin 0 is a valid pin, so whatever is connected (such as your USB serial connection) would be affected.

What I meant to say was that for your purposes, you need to distinguish entries that are valid vs entries that are invalid.

#define NUM_ENTRIES 5
byte myPinNumbers[NUM_ENTRIES] = { 8, 9, 10, [glow]-1[/glow], 13 };
byte myDesiredOutputs[NUM_ENTRIES] = { HIGH, HIGH, LOW, [glow]LOW[/glow], HIGH };

int i;
for (i = 0; i < NUM_ENTRIES; i++) {
    [glow]if (myPinNumbers[i] >= 0)[/glow]
        digitalWrite(myPinNumbers[i], myDesiredOutputs[i]);

The highlighted parts look for “invalid” pin numbers and don’t try to output anything for those entries.

Okay, I'm on it. Thanks again for the clarification.