![](templates/subSilver/images/CCSLogo.jpg) |
![CCS C Software and Maintenance Offers](templates/subSilver/images/forumAd6.jpg) |
View previous topic :: View next topic |
Author |
Message |
XorpiZ Guest
|
Problems with strtok() |
Posted: Sat Nov 26, 2005 8:19 am |
|
|
Hi, i'm having a few problems with strtok().
I've tried using the example given in the reference sheet.
My program looks like this (plus the defines, includes etc)
Code: |
void main()
{
char raw_nmea_data [65];
char term;
char *ptr;
strcpy(raw_nmea_data, "$GPRMC,150022.721,V,0000.0000,N,00000.0000,E,0.00,0.00,251105,,*16");
//strcpy(raw_nmea_data, "one, two, three");
term = ',';
ptr = strtok(raw_nmea_data, term);
while(ptr != NULL)
{
puts(ptr);
ptr = strtok(NULL, term);
}
while(1);
} |
See, now the problem is. If i use "strcpy(raw_nmea_data, "$GPRMC,150022.721,V,0000.0000,N,00000.0000,E,0.00,0.00,251105,,*16");
the output on hyperterm looks like this
$GPRMC
7
V
N
And thats obviously wrong..
But if i use "strcpy(raw_nmea_data, "one, two, three"); it looks like this
one
two
three
Just like it's supposed to. Why doesn't the other one work?
Any hints, tips, pointers etc. very much appreciated [/code] |
|
![](templates/subSilver/images/spacer.gif) |
Guest
|
|
Posted: Sat Nov 26, 2005 8:55 am |
|
|
Well I figured it out..
use strcpy(term, ","); instead of term = ',' and char term[2] instead of char term; |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah Guest
|
|
Posted: Sat Nov 26, 2005 10:03 am |
|
|
It makes sense when you realise the a 'string' in C, is a sequence of characters, with a null terminator. Your previous declarations, declared a character, but not a 'string'. You can just declare:
char term[2] = ",";
Which will declare the string, and pre-initialise it with the required value,all in one go. :-)
Best Wishes |
|
![](templates/subSilver/images/spacer.gif) |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|