jacketizer / libnmea

Lightweight C library for parsing NMEA 0183 sentences
MIT License
260 stars 92 forks source link

gprmc struct can't access 2 internal members in a single printf call #13

Closed andrewhodel closed 6 years ago

andrewhodel commented 7 years ago

This doesn't work, it only prints the latitude.

        if (nmeadata->type == NMEA_GPRMC) {
            nmea_gprmc_s *gprmc = (nmea_gprmc_s *) nmeadata;
            printf("Latitude: %5f\t Longitude: %5f\n", gprmc->latitude, gprmc->longitude);
        }

However if you print the longitude with another printf call, it works.

jacketizer commented 7 years ago

The structure of the gprmc data is:

typedef struct {
    nmea_s base;
    nmea_position longitude;
    nmea_position latitude;
    struct tm time;
} nmea_gprmc_s;

And the members logitude and latitude:

typedef struct {
    double minutes;
    int degrees;
    nmea_cardinal_t cardinal;
} nmea_position;

This means that you should do the following to print the positions using printf:

printf("Longitude:\n");
printf("  Degrees: %d\n", gprmc->longitude.degrees);
printf("  Minutes: %f\n", gprmc->longitude.minutes);
printf("  Cardinal: %c\n", (char) gprmc->longitude.cardinal);
printf("Latitude:\n");
printf("  Degrees: %d\n", gprmc->latitude.degrees);
printf("  Minutes: %f\n", gprmc->latitude.minutes);
printf("  Cardinal: %c\n", (char) gprmc->latitude.cardinal);
andrewhodel commented 6 years ago

If 2 members of nmea_gprmc_s have the same typedef, why wouldn't they both print the same?