m1geo
Joined: 31 Dec 2008 Posts: 5 Location: London, England, UK
|
Pointers & Strtok() Confusion |
Posted: Thu Jan 01, 2009 2:16 am |
|
|
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
|
|
Posted: Thu Jan 01, 2009 2:53 am |
|
|
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 |
|