How to determine array length of uint8_t?
Asked Answered
I

4

5

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;
}
Inclose answered 16/4, 2015 at 11:17 Comment(2)
I don't need msg[7] for now, if I have to use it to find the length, then I can uncomment itInclose
The length of msg is 8, whether you initialize all the elements or not.Godinez
I
7

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.
Ideality answered 16/4, 2015 at 11:26 Comment(6)
I want to find out length of the array, not the size of the arrayInclose
There is no such thing in C.Photogrammetry
@Inclose : what do you mean ? This ratio gives the length of your array (like .length in Javascript).Ideality
Amessihel, I don't know Javascript but I guess something similar is what I want. However, I want to find out if it is possible to do it even the mid elements are '\0'Inclose
@Inclose So use the sizeof solution, but only in the block where the array is defined. Otherwise define a struct with a size_t member which stores the size.Ideality
msg[2] to msg[5] are initialized to 0, which equals to '\0'.Luik
P
1

When passing array to a function it decays to a pointer, and there is no way in function to know the length of your original array. Pass the length as additional variable

int osal_DataLength( char *pString, int size )
. 
.
.
printf("the array length:%d\n", osal_DataLength(msg, sizeof(msg)) );
Podvin answered 16/4, 2015 at 11:21 Comment(0)
M
0
strlen(const char *str)

Strlen function is looking for '\0' to finish and show array length. So you are doing all array datas firstly '\0' then in your setNewLevel function msg[0]= 0 and msg[1] = 7 and then msg[6] = newLevel but others are '\0' so you are always see length is 2 .

Morphophonemics answered 16/4, 2015 at 11:40 Comment(1)
Do I need to arbitrary assign values to the 2nd, 3rd,4th, and 5th elements of the array in order to detect the length of the array is 7 on the above code?Inclose
M
-1

Maybe you can use it like this. Firstly you can use memset to set all element arrays to ZERO"0" then you add what you want in array. You have to do last thing in array element you use '\0'. if you are use like that you can see array length.

    void setNewLevel( uint8_t newLevel ){ //GW specific
    uint8_t msg[8] = {'\0'};
    memset(msg,'0',8); //  to set all element to zero 
    msg[0] = '0';
    msg[1] = '7';
    msg[6]= newLevel;
    msg[7]= '\0';  // this is array's last element.
    printf("the array length:%d\n", osal_DataLength(msg) );
    }
Morphophonemics answered 16/4, 2015 at 12:4 Comment(1)
uint8_t msg[8] = {'\0'}; sets first element to '\0' and the rest to 0, so memset() is redundant.Luik

© 2022 - 2024 — McMap. All rights reserved.