Good day to you guys! im having a bit of a problem using strtok. im currently tinkering with extracting gps data, which are separated by commas. ive seen code in several forums but most of them assign the output of strtok to the same char pointer.
what i want to do is assign the tokens to different char pointers. the relevant part of the code is shown below. what happens is that once i try to assign the next token to char pointer ns, i get an invalid conversion from char to char* error. i dont know why this happens. any help is appreciated.

char gps[] = "$GPGGA,002153.000,3342.6618,N,11751.3858,W,1,10,1.2,27.0,M,-34.2,M,,0000*5E";
char* take_tok;
char* latitude,longitude, ns;

  take_tok = strtok (gps , ",");
  take_tok = strtok (NULL , ",");
  latitude = strtok (NULL , ",");
  ns = strtok (NULL , ",");

the problem occurs at line 8

Dani AI

Generated

As noted, the compile error comes from the declaration style: a single asterisk in a comma-separated list only applies to the identifier that immediately follows it, so the other names become plain char. A few practical points that add to that correction and help this NMEA parsing scenario:

  • Declare each token pointer explicitly, and ensure the source is a modifiable array (strtok writes NULs into the buffer). Always check strtok return values before using them.
  • Tokens returned by strtok point inside the original buffer. Make independent copies if the original buffer will be reused or freed.

Example (safe pointer declarations and guarded token extraction):

char nmea[] = "$GPGGA,..." ;   /* modifiable array */
char *tok = NULL;
char *lat = NULL;
char *ns = NULL;

tok = strtok(nmea, ",");        /* message id */
if (tok) tok = strtok(NULL, ",");   /* time */
if (tok) lat = strtok(NULL, ",");   /* latitude field */
if (lat) ns = strtok(NULL, ",");    /* N/S hemisphere */

Copy a token when long-lived storage is needed:

char *lat_copy = NULL;
if (lat) {
    lat_copy = malloc(strlen(lat) + 1);
    if (lat_copy) strcpy(lat_copy, lat);   /* remember to free(lat_copy) */
}

Brief note on numeric conversion: NMEA latitude like 3342.6618 is ddmm.mmmm. Convert with:

double v = strtod(lat_copy, NULL);
int deg = (int)(v / 100);
double minutes = v - deg * 100;
double decimal = deg + minutes / 60.0;
if (ns && *ns == 'S') decimal = -decimal;

For thread-safety or reentrant code, prefer strtok_r (POSIX) or strsep where available. For : after fixing declarations, add NULL checks and copy tokens when needed; that will remove the invalid-conversion error and avoid runtime surprises.

Recommended Answers

All 3 Replies

Yes, you can assign different pointer, however you are not doing so.

char* latitude,longitude, ns;

That expands to

char *latitude;
char longitude;
char ns;

Do you see the problem? Only the first declaration is a pointer, the rest are just regular chars.

thank you very much sir!

that's "ma'am"

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.