Access the answers to your technology questions today.
Subscribe Now
30-day free trial. Register in 60 seconds.
What Makes Experts Exchange Unique?
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.
Try it out and discover for yourself.
Subscribe Now
30-day free trial. Register in 60 seconds.
Join the Community
Give a Little. Get a Lot.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Join the Community
by: Infinity08Posted on 2007-05-26 at 04:46:35ID: 19161675
int main(void) {
---------- ---------- ---------- ---------- ---------- -------- ---------- ---------- ---------- ---------- ---------- --------
---------- ---------- ---------- ---------- ---------- -------- ---------- ---------- ---------- ---------- ---------- --------
char *token;
char *delimiter = " ;:"; /* <--- you forgot a ; at the end of the line. Also : you only need one space in the string */
char str[16] = "Hello; Everyone"; /* <--- I'll explain later why I didn't use a char* */
token = strtok(str, delimiter);
while (token != NULL) {
printf("%s\n", token); /* <--- use %s to print the token. %c is just for a character. I also added a \n */
token = strtok(NULL, delimiter);
}
return 0;
}
>> I find it strange that the str is a pointer of type char.
You have to look at it this way :
"Hello; Everyone" is a string literal, and in memory that is several characters that are in consecutive locations in memory :
--------------------------
| 'H' | 'e' | 'l' | 'l' | 'o' | ';' | ' ' | 'E' | 'v' | 'e' | 'r' | 'y' | 'o' | 'n' | 'e' | '\0' |
--------------------------
Each "box" is one byte (one character). Note that the final character ('\0') is the terminator - it signifies the end of the string literal.
By doing this :
char *str = "Hello; Everyone";
you make str point to the first character like this :
--------------------------
str --> | 'H' | 'e' | 'l' | 'l' | 'o' | ';' | ' ' | 'E' | 'v' | 'e' | 'r' | 'y' | 'o' | 'n' | 'e' | '\0' |
--------------------------
In C, str can now be treated as a string (an array of characters).
>> And,we have not used malloc or calloc to allocate memory
That's because the string literal has been allocated automatically. It usually is in const memory - ie. it can't be changed. That is why in the code I replaced char* with an array of characters.
strtok() will modify the string, and since a string literal can't be modified, we need to copy the string literal to memory where it CAN be modified like this :
char str[16] = "Hello; Everyone";
This creates an array of 16 characters (15 + the terminating null character), and copies the characters from the string literal into it.
>> Also, could anyoen tell me what the problem of the code is ? Why is n't it running ?
See the modifications I made to the code :
1) A missing ;
2) strtok() tries to modify a string literal - we don't want to do that ...
3) token is a string ... you can't print a string with %c (a character), but need to use %s instead.