Determining array length

I'm trying to make a function to read the length of an array that is not created at compile time, but am having some trouble with it. My code looks something like this

char testdata[ ] = {0x09, 0x00, 0x56, 0x79, 0xC8}; //this is just for testing, real data is created during the program.
.
.
.
unsigned long dataLength(char data00[ ]){        //data is terminated with 0xC8
 unsigned long i0 = 0;
  while (data00[i0] != 0xC8)                            //read everything up until 0xC8
  {Serial.write(data00[i0]); i0++; delay(600);}    // Serial & delay included for debugging
  return i0;                                                   //return length of the data
}

The program starts gets to 0xC8 and just keeps going. It never exits the while loop.

Any suggestions?

Use byte instead of char and it works.

I think the problem is that char is signed (+127 to -127), and the 0xC8 literal in your while comparison defaults to an int.

When you compare the array value to 0xC8, your array value is being promoted to the int representation of the negative number. The proof is that it works if you explicitly cast the literal to char:

while (data00[i0] != (char)0xC8)

But you probably want to use byte instead of char anyways.

BTW you don't need an unsigned long for your datalength - you are allowing for an array of 4,294,967,295 elements which is far beyond memory capacity.

Awesome. Thanks!

Using an unsigned log as a RAM array index on an AVR is a little optimistic, isn't it?