Arduino IDE how to properly fill an array of strings

bogosort

Joined Sep 24, 2011
696
But is still not fully clear to me how do I detect and handle null character.
A char array is not a string unless it is terminated by null (the zero byte). Without the null, string library functions won't be able to know when the string ends. When you are constructing your own char arrays, it is up to you to ensure that the array is null terminated. Some string library functions will take care of this for you, others won't.

C:
strncpy(list_of_networks[i],temp_buffer,WiFi.SSID(i).length());
If you read the man page for the strncpy function, you'll see that it does not assure the destination buffer is null terminated. What you are doing is asking strncpy to copy 13 bytes to the 256-byte list_of_networks[j] buffer. But the string you are copying is 14 bytes long: 13 characters + null termination. So, you need to either terminate the buffer yourself: list_of_networks[j][13] = 0. Or, tell strncpy to copy length() + 1 bytes.
 
Top