Search code examples
carraysstrlenuint8t

How to determine array length of uint8_t?


I'm trying to determine array length of msg on the below code. I used strlen and sizeof but they don't return 6. What function can I use to determine the length of uint8_t array or how can I modify the below code (osal_DataLenght() func)?

int osal_DataLength( char *pString ){
  return (int)( strlen( pString ) );
}

void setNewLevel( uint8_t newLevel ){ //GW specific
  uint8_t msg[8] = {'\0'};
  msg[0] = '0';
  msg[1] = '7';
  msg[6]= newLevel;
  //msg[7] = '0';
  printf("the array length:%d\n", osal_DataLength(msg) );
}

int main(void){
    setNewLevel(0xD5);
    return 0;
}

Solution

  • To know the size of your array, write (in setNewLevel() as said @Dabo) :

    sizeof(msg)/sizeof(uint8_t);
    

    strlen() returns the size of a string (char array terminated by NULL, ie '\0'). You CAN'T use it in this context, since :

    1. msg[2] to msg[5] values are not initialized
    2. msg is not a char sequence terminated by NULL.