Problem determining length of byte array

I have an array of pointers to byte arrays of varying length.

How can I determine the length of a given byte array in this scenario?

I guess I could count the array lengths by hand and store them in another array, but that seems like an undue amount of work doing something (counting) that computers excel at automating! :smiley:

Below code illustrates the problem:

void setup()
{
  Serial.begin(9600); // for debugging

  byte zero[] = {8, 169, 8, 128, 2,171,145,155,141,177,187,187,2,152,2,8,134,199};
  byte one[]  = {8, 179, 138, 138, 177 ,2,146, 8, 134, 8, 194,2,1,14,199,7, 145, 8,131, 8,158,8,187,187,191};
  byte two[] = {29,7,1,8, 169, 8, 128, 2,171,145,155,141,177,187,187,2,152,2,8,134,199, 2, 2, 8, 179, 138, 138, 177 ,2,146, 8, 134, 8, 194,2,1,14,199,7, 145, 8,131, 8,158,8,187,187,191};

  byte* numbers[3] = {zero, one, two };
  
  function(numbers[1], sizeof(numbers[1])/sizeof(byte)); //doesn't work as desired, always passes 2 as the length
  function(numbers[1], 25); //this works
}

void loop() {
}

void function( byte arr[], int len )
{
  Serial.print("length: ");
  Serial.println(len);
  for (int i=0; i<len; i++){
    Serial.print("array element ");
    Serial.print(i);
    Serial.print(" has value ");
    Serial.println((int)arr[i]);
  }
}

Thank you Arduino gurus!

The sizeof function returns the size of an array. Divide that value by the size of an element in the array to determine the number of values in the array.

int zeroCnt = sizeof(zero) / sizeof(zero[0]);

Thank you, that is helpful.

Using your suggestion, I could populate an array of int with the lengths of the byte arrays.

Is there a way to get the length of a byte array by referencing the array of array pointers?

For example:

  int zeroCnt = sizeof(numbers[0]) / sizeof(numbers[0][0]);

yields 2, I guess that is because numbers[0] is a pointer and the length of the pointer is 2, correct?

Is there another way to get the length at runtime without pre-populating an array of lengths?

numbers[0] is a pointer to a byte. numbers[0][0] is a byte. A byte has a size of 1. A pointer is a integer, which has a size of 2. So, that's why you are getting 2 when you do:

int zeroCnt = sizeof(numbers[0]) / sizeof(numbers[0][0]);

Is there another way to get the length at runtime without pre-populating an array of lengths?

I couldn't figure out a way.

I went over to StackOverflow and posted this question: c - Getting size of a specific byte array from an array of pointers to bytes - Stack Overflow and got back a pretty cool solution that I verified works on the Arduino:

void setup(void)
{
    ...

    byte* numbers[3] = {zero, one, two };
    size_t sizes[3] = {sizeof(zero), sizeof(one), sizeof(two)};

    function(numbers[1], sizes[1]);
}

Thanks for pointing me in the right direction Paul! :slight_smile:

Two comments. First, that does not avoid the need to pre-define an array of lengths.

Second, sizeof(one) returns the correct length of the array named one only if the size of each item in the array is one. You would still need divide by the size of an element in the array if the size of an element is not 1.

Hi Paul, you are correct.

Although I need to define an array of sizes, I avoid the PITA of having to count sizes myself and hand code the size values into the array of sizes. I have a couple dozen of these to do, and some are over 100 bytes, so I know I would make a mistake in counting. And yes I understand the need to divide by the size of an element. Got it!

If you're worried about repeated typing of "sizeof .../ sizeof..." like:sizeof(numbers[0]) / sizeof(numbers[0][0]);
just define yourself a macro:

#define SIZEOF_ARRAY(x)  (sizeof (x) / sizeof (x[0]))

thus:

SIZEOF_ARRAY(numbers[0])