The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Operating Systems > Linux
.
google unix.com



Linux RedHat, Ubuntu, SUSE, Fedora, Debian, Mandriva, Slackware, Gentoo linux, PCLinuxOS. All Linux questions here!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Passing global variable to a function which is called by another function sars Shell Programming and Scripting 4 06-30-2008 11:39 AM
strtok equivalent in perl jisha Shell Programming and Scripting 1 05-06-2008 03:38 AM
Regardign strtok() output directing to 2-D string array SankarV High Level Programming 3 04-28-2008 09:48 AM
Function within function (Recurance) chassis UNIX for Dummies Questions & Answers 2 09-19-2006 09:32 AM
better way than strtok? annie High Level Programming 7 10-05-2005 02:01 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 01-24-2008
Tanvirk Tanvirk is offline
Registered User
  
 

Join Date: Jan 2008
Posts: 8
Strtok function....

can any help me out y dis program is giving me a segmentation fault.....

#include<stdio.h>
#include<string.h>
int main()
{
char *str="Tanvir/home/root/hello";
const char *d ="/";
char *ret;


ret=strtok(str,d);
if(ret==NULL)
printf("NULL NULL");
else
printf("\n%c",ret);

return 0;
}
  #2 (permalink)  
Old 01-24-2008
ennstate ennstate is offline
Registered User
  
 

Join Date: Mar 2007
Location: Chennai
Posts: 222
Quote:
Originally Posted by Tanvirk View Post
can any help me out y dis program is giving me a segmentation fault.....

#include<stdio.h>
#include<string.h>
int main()
{
char *str="Tanvir/home/root/hello";
const char *d ="/";
char *ret;


ret=strtok(str,d);
if(ret==NULL)
printf("NULL NULL");
else
printf("\n%c",ret);

return 0;
}
In this case str points to location that has the initialized value which could be either ready only or read writable.If it happens to be on read only then the strtok function may not be able to modify the string and so generates the SEGV

You could change that to,
Code:
char  str[]="Tanvir/home/root/hello";
Thanks
Nagarajan G
  #3 (permalink)  
Old 01-24-2008
ashokmeti ashokmeti is offline
Registered User
  
 

Join Date: Jan 2008
Location: Bangalooru
Posts: 15
Quote:
Originally Posted by ennstate View Post
In this case str points to location that has the initialized value which could be either ready only or read writable.If it happens to be on read only then the strtok function may not be able to modify the string and so generates the SEGV

You could change that to,
Code:
char  str[]="Tanvir/home/root/hello";
Thanks
Nagarajan G
Dear Nagarajan,

Thanks for ur detailed explaination of strtok.
I fully agree with ur comment.

but however strtok should not be used in threads. can u explain me why?
they say in such case its safe to use strtok_r().

Regards
-Ashok Meti
  #4 (permalink)  
Old 01-24-2008
Tanvirk Tanvirk is offline
Registered User
  
 

Join Date: Jan 2008
Posts: 8
Thanks a lot Nagrajan and Ashok !!
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 05:53 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0