View Full Version : strtok function in c ..help
abdullah
10-28-2003, 04:56 AM
hi,
i am experimenting with the strtok function in c and when i compile my code i dont get any error ..but when i try to run it , i get a segmentation fault. can anyone please point me in the right direction? thanks
the code i am trying to run is:
#include <stdio.h>
#include <stdlib.h>
int main ()
{
char * haystack = "hi how are you";
char * token;
token = strtok(haystack, '/');
while (token != NULL) {
printf("The token is: %s \n", token);
printf("The haystack is: %s \n",haystack);
token = strtok(haystack, '/');
}
return 0;
}
ibrahim34
10-13-2004, 08:21 PM
Assalam Aleykoum
char * haystack = "hi how are you";
char * token;
token = strtok(haystack, '/');
First , there is a memory allocation problem with this because generaly when you make char * ="something" you create a pointer to a read only memory ( depending of the OS ..) , maybe const char * ="your_string" can solve the problem.
or you can declare haystack on the stack like this and it will works :
char haystack[] = "This code works.";
char * token;
token = strtok(haystack, " ");
And then ( second problem ) :
After a first call to strtok, the function may be called with NULL as string parameter, and it will follow by where the last call to strtok found a delimiter.
delimiters may vary from a call to another.
http://www.cplusplus.com/ref/cstring/strtok.html
finally these two code works fine
1 ) static allocation of haystack
int main()
{
char haystack[] = "This code works.";
char * token;
token = strtok(haystack, " ");
while (token != NULL) {
printf("The token is: %s \n", token);
token = strtok(NULL, " ");
}
return 0;
}
2 ) dynamic allocation of haystack
int main( )
{
char string[] = "This code works";
char *haystack = NULL;
char * token = NULL;
haystack = (char*) malloc (sizeof(string));
strcpy(haystack,string);
token = strtok(haystack, " ");
while (token != NULL) {
printf("The token is: %s \n", token);
token = strtok(NULL, " ");
}
return 0;
}
Wa Salam
furqan
10-16-2004, 09:34 AM
Looks like my C needs brushin up lol... Good work...
abdullah
10-16-2004, 07:59 PM
thanks for the reply bro.. i just saw it
ibrahim34
10-19-2004, 01:20 PM
Salams
you may find that usefull too :
http://www.sgi.com/tech/stl/basic_string.html
It is the STL string class and it is a very powerful class to handle and manipulate sequence of characters in C++
awara
03-30-2005, 04:13 PM
salaam guys,
juzz a small contribution.
haystack = (char*) malloc (sizeof(string));
could be
haystack = (char*) malloc (sizeof(string)+1);
when copying a string which is done in the next step, always add one for the null terminated string :), or heap cries in the long run.
and juzz a free(haystack) before return 0.
Holy God, did i get into troubles cuz of these mem. allocation things. haunts me now, newayz, cheer up.
salaam alaikum.
Assalam Aleykoum
2 ) dynamic allocation of haystack
int main( )
{
char string[] = "This code works";
char *haystack = NULL;
char * token = NULL;
haystack = (char*) malloc (sizeof(string));
strcpy(haystack,string);
token = strtok(haystack, " ");
while (token != NULL) {
printf("The token is: %s \n", token);
token = strtok(NULL, " ");
}
return 0;
}
Wa Salam
Powered by vBulletin™ Version 4.0.1 Copyright © 2010 vBulletin Solutions, Inc. All rights reserved.