CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Problems with strtok()

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
XorpiZ
Guest







Problems with strtok()
PostPosted: Sat Nov 26, 2005 8:19 am     Reply with quote

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 Smile[/code]
Guest








PostPosted: Sat Nov 26, 2005 8:55 am     Reply with quote

Well I figured it out..

use strcpy(term, ","); instead of term = ',' and char term[2] instead of char term;
Ttelmah
Guest







PostPosted: Sat Nov 26, 2005 10:03 am     Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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