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

Pointers & Strtok() Confusion

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



Joined: 31 Dec 2008
Posts: 5
Location: London, England, UK

View user's profile Send private message Visit poster's website MSN Messenger

Pointers & Strtok() Confusion
PostPosted: Thu Jan 01, 2009 2:16 am     Reply with quote

Hello All.

I am sure this is obvious if you know how, but I (apparently) don't! I am trying to tokenise a string with the strtok() function. I have an string of 128 elements (char myString[128]), and I am trying to basically split the string into smaller seperate chunks:

for example, if I filled myString like:
Code:

strcpy(myString, "GEORGE,ALICE,LINDA,CHRISTOPHER,DAVID,AARON,PETE,DEBBIE");


How do i go about getting each token into a new variable? I know that there will be a fixed number of tokens, but the length of each may vary (the names may be in any order).

I require say 8 strings, each cointaing the data between the 7 commas, somthing like:

sFirst = "GEORGE";
sSecond = "ALICE"; (in psuedo code - for explaination)
sThird = "LINDA";

I can then print out what i need. The problem I am having is that strtok() returns pointers to strings. I am aware that the pointer is to the first character after the deliminator (comma), so if i was to print the values of the pointers in myString, i get "GALCDAPD". However, i dont know how to offset the pointer to get the second character.

Ive tried something like *ptr++, ptr++, and &ptr++. I am not sure that this method would work. If it can be done like this, I can simply run a for-loop between the two pointer addresses, and write the pointer value into a new array.

However, I am confused. I searched the forum well, and Googled too, and couldn't find an answer to this. Please don't think I didn't look.

Kind Regards, and Thanks for your time in reading!
_________________
George Smart

Student in Electronic & Electrical Engineering
University College London
m1geo



Joined: 31 Dec 2008
Posts: 5
Location: London, England, UK

View user's profile Send private message Visit poster's website MSN Messenger

PostPosted: Thu Jan 01, 2009 2:53 am     Reply with quote

I am sorry. I have solved the problem.

I didn't realise that strtok() terminated the string on the delimiter, so you could just read the modified string.

Happy new year all!
_________________
George Smart

Student in Electronic & Electrical Engineering
University College London
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