Next item in array (rollover)

Hi,
I am trying to program a function to calculate the next item in an array. For the last item the next one should be 0.
This should be simple, but I don't know how to check for the last item.

int nextItem(int item)
{
  if (array[item + 1]) {
    return item + 1;
  } else {
    return 0;
  }
}

I have read some posts about calculating the length of the array by its memory consumption, but isn't there an easier way?

Kevin

Not entirely sure what you are trying to achieve. Can you expand on it a little?

I think you are confusing two different concepts...

One is initializing an array and knowing the size of memory stored in it, the other is knowing the valuable data you have in it.

This will probably give you enough insight.

Are you looking for the modulo operator "%" ?

KevinT:
I have read some posts about calculating the length of the array by its memory consumption, but isn't there an easier way?

Absolutely. If you know how many items are in it.

I think I was a bit unclear.
I like to calculate a simple difference:

int diff = array[actItem + 1] - array[actItem];

As this code will not work if actItem is the last item in the array I need something like this:

int nextItem(int item)
{
  if (exist(array[item + 1])) {
    return item + 1;
  } else {
    return 0;
  }
}

int diff = array[nextItem(actItem)] - array[actItem];

Hopefully this is a bit more clearly.

A simple way is to make the array one element bigger than you need and put a zero in it. That way there is no 'special' case to consider.

nItems = sizeof(array) / sizeof(array[0]);

int nextItem(int item)
{

  if (item == nItems -1)
     // last item, do nothing
     return 0;

    ....
}

Rob

Is there a way to pass on the pointer to an array to merge this code into one function?

Is this correct?

int nItems (byte *pArray)
{
  return sizeof(*pArray) / sizeof(*pArray[0]);
}

byte array[] = {1,2,3,4,5};
int number = nItems(array);
// number should be 5

No. Your example is taking the size of a pointer (2 bytes on an arduino). A pointer is nothing but an address of a memory location - there is no extra information carried about what it's pointing to, such as the size of an array. Rob's example works because he is taking the size of an array that is known at compile time. Once you pass a pointer to that array to a function though, the function has no idea how large the array is.

But if there isn't a way to get the current size of the array how does this code handle an array that gets its items during runtime?

You could pass the size of the array, along with the array.

so thats what I will do.

Thanks to all.

Could someone give me a little more explanation on this? I am posting what I understand but, some of this I do not understand. I need help with part with the question marks.

int array[6] = {2, 4, -8, 3, 2};  //array to be measured by sizeof

nItems = sizeof(array) / sizeof(array[0]);
//nltems = 6 divided by 2?????????
int nextItem(int item)
{

  if (item == nItems -1)
 //if the int item equals the result for nltems - 1 do action?????
     // last item, do nothing
     return 0;

    ....
}

Thanks,
CG

Where does the 6 come from?

AWOL:
Where does the 6 come from?

This my array. I think the sizeof would be 6. It contains 5 ints and one null.

int array[6] = {2, 4, -8, 3, 2};  //array to be measured by sizeof

Therefore I put that piece into my thoughts of what the code meant.

nItems = sizeof(array) / sizeof(array[0]);
//nltems = 6 divided by 2?????????

I got the number 2 from the first element of array location [0].

Is this correct?

Testing different variables in a 10 element array.

Shows the size in bytes of a single entry, and the size in bytes of the whole array.

#define ASIZE 10

char _char[ASIZE];
int _int[ASIZE];
long _long[ASIZE];
float _float[ASIZE];

void setup()
{
  Serial.begin(9600);
  Serial.print("Char  - Type: ");
  Serial.print(sizeof(_char[0]));
  Serial.print(" Array: ");
  Serial.println(sizeof(_char));
  
  Serial.print("Int   - Type: ");
  Serial.print(sizeof(_int[0]));
  Serial.print(" Array: ");
  Serial.println(sizeof(_int));

  Serial.print("Long  - Type: ");
  Serial.print(sizeof(_long[0]));
  Serial.print(" Array: ");
  Serial.println(sizeof(_long));

  Serial.print("Float - Type: ");
  Serial.print(sizeof(_float[0]));
  Serial.print(" Array: ");
  Serial.println(sizeof(_float));

}

void loop()
{
}

Results in:

Char  - Type: 1 Array: 10
Int   - Type: 2 Array: 20
Long  - Type: 4 Array: 40
Float - Type: 4 Array: 40

Sorry, skipped the specified size and just counted the elements.

sizeof returns size in bytes, so in your example, your array of 6 ints occupies 12 bytes. Hence, you have to divide it by the size of the first (or any other) element - two, to get the number of items it contains.

Are you looking for the value of the next item in the array, or the index of the next item in the array?

The index would be:

// item is any index into the array
// nItems is the number of elements in the array
(item + 1) % nItems;

To use the value is then trivial:

// actItem is an index into the array
// nItems is the number of elements in the array
int diff = array[(actItem + 1) % nItems] - array[actItem];