Getting size of a specific byte array from an array of pointers to bytes

Posted by Pat James on Stack Overflow See other posts from Stack Overflow or by Pat James
Published on 2010-04-23T00:01:29Z Indexed on 2010/04/23 0:03 UTC
Read the original article Hit count: 311

Filed under:
|

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(numbers1)/sizeof(byte)) doesn't work because numbers1 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.

© Stack Overflow or respective owner

Related posts about c

    Related posts about arduino