954,492 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Help Regarding strtok function

Hello All

i am trying to compile a simple code for the strtok but getting the error as

segmentation fault ( core dumped ) .. any help would be appreciated.

#include <stdio.h>
#include  <netinet/in.h>
#include <stdlib.h>
#include <string.h>

main()
{
	char *test="asim.nazeer.helo.me";
	char *rsl=strtok(test,".");	
	//while(1)
	//{	
		
		printf("%s",rsl);
	//}
		
}
asimnazeer
Newbie Poster
4 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
 

It would appear that the memory space on which the string is stored is not modifiable. But that's not expert opinion, rather just from empirical testing. Can't memset the location, so therefore it's not modifiable. And strtok modifies the string you pass it.

You can just make test as an array.

asrockw7
Junior Poster in Training
56 posts since Apr 2011
Reputation Points: 10
Solved Threads: 3
 

it works with the simple array but i donot understand y its not working like this .. actually in my senario i am getting the data in the above format..so i m stuck in it ..

asimnazeer
Newbie Poster
4 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
 

You just need to have it on real allocated memory. If you really want a pointer then strcpy it to a calloc'd pointer.

asrockw7
Junior Poster in Training
56 posts since Apr 2011
Reputation Points: 10
Solved Threads: 3
 
It would appear that the memory space on which the string is stored is not modifiable.


Correct. String literals are defined as read-only by the language, and strtok() modifies its argument.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

i donot know what to do can u explain me by the help of code please.

asimnazeer
Newbie Poster
4 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
 
i donot know what to do can u explain me by the help of code please.


You have no choice but to make sure the source string passed to strtok() is modifiable. That means either storing it initially in an array:

#include <stdio.h>
#include <string.h>

int main(void)
{
    char src[] = "asim.nazeer.helo.me";
    char *tok = strtok(src, ".");
    
    while (tok != NULL) {
        printf("'%s'\n", tok);
        tok = strtok(NULL, ".");
    }

    return 0;
}

Or making a copy if you don't control the string provided or need to retain the original string for any reason:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void foo(const char *s) // Assuming no control over s
{
    char *copy = malloc(strlen(s) + 1);
    char *tok;
    
    if (copy == NULL)
        return;
        
    strcpy(copy, s);
    
    for (tok = strtok(copy, "."); tok != NULL; tok = strtok(NULL, "."))
        printf("'%s'\n", tok);
}

int main(void)
{
    char *src = "asim.nazeer.helo.me";

    foo(src);

    return 0;
}
Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

Thanx Alot for your help it works now and i got the point y i was getting the error :)

asimnazeer
Newbie Poster
4 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: