tags:

views:

233

answers:

1

In the following example c code, used in an Arduino project, I am looking for the ability to get the size of a specific byte array within an array of pointers to bytes, for example

    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]);
      }
    }

In this code, I understand that sizeof(numbers[1])/sizeof(byte) doesn't work because numbers[1] is a pointer and not the byte array value.

Is there a way in this example that I can, at runtime, get at the length of a specific (runtime-determined) byte array within an array of pointers to bytes? Understand that I am limited to developing in c (or assembly) for an Arduino environment.

Also open to other suggestions rather than the array of pointers to bytes. The overall objective is to organize lists of bytes which can be retrieved, with length, at runtime.

+3  A: 
void setup(void)
{
    ...

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

    function(numbers[1], sizes[1]);
}
ndim
Works great, thank you very much!
Pat James