'Having issues while using %s
I am working on a project in which I extract data received from GPS and save it in array named rx_data_buffer:
uint8_t rx_data_buffer[84] = {0};
The result I get is as below(see rx_data_buffer) : enter image description here
Then I extract the data from rx_data_buffer in following arrays:
uint8_t lattitude[10] = {0};
uint8_t longitude[10] = {0};
uint8_t altitude[5] ={0};
The data is extracted as follows:
for (uint8_t i =0; i < 10; i++)
{
lattitude[i] = rx_data_buffer[i+17];
}
for (uint8_t j =0; j < 10; j++)
{
longitude[j] = rx_data_buffer[j+31];
}
for (uint8_t k =0; k < 5; k++)
{
altitude[k] = rx_data_buffer[k+56];
}
The results are as follows:
Till here everything is working fine but when I put the latitude, longitude and altitude data in an array using %s for the purpose of saving them in SD card as follow:
snprintf(mystring, sizeof(mystring), "\n%.2f,%.2f,%.2f,%.2f,%.2f,%.2f,%.2f,%.2f,%.2f,%d,%s,%s,%s", readAccelerometer(X_AXIS),readAccelerometer(Y_AXIS),readAccelerometer(Z_AXIS),readGyro(X_AXIS), readGyro(Y_AXIS),readGyro(Z_AXIS),readMagnetometer(X_AXIS),readMagnetometer(Y_AXIS), readMagnetometer(Z_AXIS),heart_rate,longitude,lattitude,altitude);
I get the longitude and altitude values fine but am getting wrong latitude value in a sense that it show both the values combine as shown below:enter image description here.
What can be the issue?
Solution 1:[1]
In order to be used as strings, the arrays need a null terminator. Since you are initializing the arrays to zero, the simplest solution is to make them one larger than you need. eg:
uint8_t lattitude[11] = {0};
uint8_t longitude[11] = {0};
uint8_t altitude[6] ={0};
As long as you never write over the last element in the array, it will remain '\0' and you can use the string functions.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | William Pursell |
