473,378 Members | 1,391 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

strtok causes Segmentation fault

I need to delimit a string. The delimiters are a semicolon and comma.
When I run the program I get a segmentation fault on the first strtok.
I followed the examples of others and from my old C books, but I can't
seem to find the problem. The accesslist has a format of
20,45;22,44;46,28;99,43,etc. What am I doing wrong?
Thanks,

#include <sys/signal.h>

#include <messages.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <ctype.h>

#define or ||
#define and &&

int createvarible(const int sock, char *name, char *type, char *value,
char *list)
{
int n=0, size, returnval=0;
char *tokenptr=NULL, *tokenptr2=NULL;
int accesscode, portnum;

tokenptr = strtok(list, ";"); //this line causes the seg fault WHY?

if (tokenptr == NULL) {
tokenptr2 = strtok(list, ",");//delimit accesscode as there is no
semicolon
accesscode=atoi(tokenptr2);
tokenptr2 = strtok(NULL, ",");//delimit port
portnum=atoi(tokenptr2);
printf("code[0] %d\n", accesscode);
printf("port[0] %d\n", portnum); }
else {
while (tokenptr != NULL) { //don't worry about the code down here it
does what I need
tokenptr2 = strtok(list, ";");//delimit accesscode
accesscode=atoi(tokenptr2);
tokenptr2 = strtok(NULL, ",");//delimit portnum
portnum=atoi(tokenptr2);
printf("code[0] %d\n",accesscode);
printf("port[0] %d\n",portnum);
n++;
}
}
return accesscode;
}

May 11 '06 #1
17 5154
bo******@hotmail.com wrote:
I need to delimit a string. The delimiters are a semicolon and comma.
When I run the program I get a segmentation fault on the first strtok.
I followed the examples of others and from my old C books, but I can't
seem to find the problem. The accesslist has a format of
20,45;22,44;46,28;99,43,etc. What am I doing wrong?
Thanks,

#include <sys/signal.h>

#include <messages.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <ctype.h>

#define or ||
#define and &&

int createvarible(const int sock, char *name, char *type, char *value,
char *list)
{
int n=0, size, returnval=0;
char *tokenptr=NULL, *tokenptr2=NULL;
int accesscode, portnum;

tokenptr = strtok(list, ";"); //this line causes the seg fault WHY?

Is list non-null when first used as an arg to strtok()? Is it
null-terminated?
May 11 '06 #2

"void * clvrmnky()" <cl**************@hotmail.com.invalid> wrote in message
news:KE******************@nnrp.ca.mci.com!nnrp1.uu net.ca...
bo******@hotmail.com wrote:
I need to delimit a string. The delimiters are a semicolon and comma.
When I run the program I get a segmentation fault on the first strtok.
I followed the examples of others and from my old C books, but I can't
seem to find the problem. The accesslist has a format of
20,45;22,44;46,28;99,43,etc. What am I doing wrong?
Thanks,

#include <sys/signal.h>

#include <messages.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <ctype.h>

#define or ||
#define and &&

int createvarible(const int sock, char *name, char *type, char *value,
char *list)
{
int n=0, size, returnval=0;
char *tokenptr=NULL, *tokenptr2=NULL;
int accesscode, portnum;

tokenptr = strtok(list, ";"); //this line causes the seg fault WHY?

Is list non-null when first used as an arg to strtok()? Is it
null-terminated?


Non Standard stuff. Could this question have been framed so as to be
topical in clc? Joe
May 11 '06 #3
Here is how I call createvarible:
createvarible(ss, "xyz", "string", "this is a test",
"22,43;44,33")
So to answer your question the string is null terminated. To null
terminate the string I think I have to put \0 at the end right?

Thanks,

May 11 '06 #4
Changing the function call to createvarible(ss, "xyz", "string", "this
is a test",
"22,43;44,33\0") still causes a seg fault.

May 11 '06 #5

bo******@hotmail.com wrote:
Changing the function call to createvarible(ss, "xyz", "string", "this
is a test",
"22,43;44,33\0") still causes a seg fault.


strtok is a surgical function i.e. it destroys the original string that
it works on.
It may not be able to change a const string.

Check these two pieces of code:

p1:

int main(void) {
char *a="something";
a[1]='b';
return 0;
}

p2:

int main(void) {
char a[]="something";
a[1]='b';
return 0;
}

See which one segfaults.

--
Ioan - Ciprian Tandau
tandau _at_ freeshell _dot_ org (hope it's not too late)
(... and that it still works...)

May 11 '06 #6
Joe Smith wrote:
"void * clvrmnky()" <cl**************@hotmail.com.invalid> wrote in message
news:KE******************@nnrp.ca.mci.com!nnrp1.uu net.ca...
bo******@hotmail.com wrote:
I need to delimit a string. The delimiters are a semicolon and comma.
When I run the program I get a segmentation fault on the first strtok.
I followed the examples of others and from my old C books, but I can't
seem to find the problem. The accesslist has a format of
20,45;22,44;46,28;99,43,etc. What am I doing wrong?
Thanks,

#include <sys/signal.h>

#include <messages.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <ctype.h>

#define or ||
#define and &&

int createvarible(const int sock, char *name, char *type, char *value,
char *list)
{
int n=0, size, returnval=0;
char *tokenptr=NULL, *tokenptr2=NULL;
int accesscode, portnum;

tokenptr = strtok(list, ";"); //this line causes the seg fault WHY?

Is list non-null when first used as an arg to strtok()? Is it
null-terminated?


Non Standard stuff. Could this question have been framed so as to be
topical in clc? Joe

strtok() is part of string.h. Its behaviour is undefined if called
immediately with NULL (i.e., no valid string used first to prime the
internal objects it uses on subsequent calls).

This is regardless of how "standard" the rest of the code is.
May 11 '06 #7
I understand strtok seg faulting if the FIRST call is with NULL, but in
my case the first call is not NULL. It is
tokenptr = strtok(list, ";"); //this line causes the seg fault WHY?
list is a varible passed to the function.

Thanks,

May 11 '06 #8
bo******@hotmail.com wrote:

Posted elsewhere:

Here is how I call createvarible:
createvarible(ss, "xyz", "string", "this is a test",
"22,43;44,33")
I understand strtok seg faulting if the FIRST call is with NULL, but
in my case the first call is not NULL. It is
tokenptr = strtok(list, ";"); //this line causes the seg fault WHY?
list is a varible passed to the function.


You are passing a string literal as the list parameter, which you then
send to strtok(). This causes undefined behavior.

Also, read the information below.

Brian
--
Please quote enough of the previous message for context. To do so from
Google, click "show options" and use the Reply shown in the expanded
header.
May 11 '06 #9
> > I understand strtok seg faulting if the FIRST call is with NULL, but
in my case the first call is not NULL. It is
tokenptr = strtok(list, ";"); //this line causes the seg fault WHY?
list is a varible passed to the function.


You are passing a string literal as the list parameter, which you then
send to strtok(). This causes undefined behavior.

So?? I tried strtok(*list, ";"); and it still seg faults. According
to my old C book strtok is supposed to take a string for the first
argument. I even tried changing createvarible to:
int createvarible(const int sock, char *varname, char *vartype, char
*initialvalue, char accesslist[64])
and I still get a seg fault. How can I get this thing to work.

May 11 '06 #10
bo******@hotmail.com wrote:
I understand strtok seg faulting if the FIRST call is with NULL,
but in my case the first call is not NULL. It is
tokenptr = strtok(list, ";"); //this line causes the seg fault
WHY? list is a varible passed to the function.


You are passing a string literal as the list parameter, which you
then send to strtok(). This causes undefined behavior.

So?? I tried strtok(*list, ";"); and it still seg faults.


What are you talking about? Do you know what a string literal is? You
can't pass anything that looks like "some text" to a function that will
modify it. You passed a string literal as the list parameter. You can't
do that.
Brian

--
Please quote enough of the previous message for context. To do so from
Google, click "show options" and use the Reply shown in the expanded
header.
May 11 '06 #11
I fixed it myself. It may not be pretty but it works. I changed
createvarible back to passing a pointer. I created a local varible
char string[64], and did a strncpy(string,list,64) and did the strtok
on string. All I can say strtok needs some improvements.

May 11 '06 #12
bo******@hotmail.com wrote:
I understand strtok seg faulting if the FIRST call is with NULL,
You are passing a string literal as the list parameter,
which you then
send to strtok(). This causes undefined behavior.

So?? I tried strtok(*list, ";"); and it still seg faults. According
to my old C book strtok is supposed to take a string for the first
argument.


So, there's more involved here than just the rules for strtok.
There's also the rules for strings.
Attempting to modify a string literal is undefined.
Seg fault is a blatant form of undefined behavior.

--
pete
May 11 '06 #13
bo******@hotmail.com wrote:
I fixed it myself. It may not be pretty but it works. I changed
createvarible back to passing a pointer. I created a local varible
char string[64], and did a strncpy(string,list,64) and did the strtok
on string.


Which is what I was trying to tell you.

Brian
--
Please quote enough of the previous message for context. To do so from
Google, click "show options" and use the Reply shown in the expanded
header.
May 11 '06 #14
bo******@hotmail.com wrote:

Here is how I call createvarible:
createvarible(ss, "xyz", "string", "this is a test",
"22,43;44,33")
So to answer your question the string is null terminated. To null
terminate the string I think I have to put \0 at the end right?


Why are you making intelligent commentary virtually impossible by
not including adequate context? See my sig below, even on the
impossible google interface to usenet. Read the referenced URLs
before posting again.

I vaguely remember you were passing some of those parameters to
strtok. strtok modifies the string on which it operates. The
strings you pass above are not modifiable, and any attempt to do so
results in undefined behaviour.

You could search the archives of this group for my "toksplit"
function, which does not have these problems, and could be launched
in that manner. If you reply (in the newsgroup) in the appropriate
format I could even be persuaded to republish it.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>
May 11 '06 #15
On 11 May 2006 12:54:32 -0700, "Nelu" <ta********@gmail.com> wrote in
comp.lang.c:

bo******@hotmail.com wrote:
Changing the function call to createvarible(ss, "xyz", "string", "this
is a test",
"22,43;44,33\0") still causes a seg fault.


strtok is a surgical function i.e. it destroys the original string that
it works on.
It may not be able to change a const string.


Your last statement is correct, but has nothing directly to do with
the OP's problem.

String literals in C have the type "array of char", and very
specifically do not have the type "array of constant char".

Attempting to modify a string literal in C produces undefined behavior
because the C standard specifically says so, not because, as you
incorrectly think, they are const qualified.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
May 12 '06 #16

Jack Klein wrote:
On 11 May 2006 12:54:32 -0700, "Nelu" <ta********@gmail.com> wrote in
comp.lang.c:

bo******@hotmail.com wrote:
Changing the function call to createvarible(ss, "xyz", "string", "this
is a test",
"22,43;44,33\0") still causes a seg fault.
strtok is a surgical function i.e. it destroys the original string that
it works on.
It may not be able to change a const string.


Your last statement is correct, but has nothing directly to do with
the OP's problem.

That's why it crashes at the first call to strtok.

String literals in C have the type "array of char", and very
specifically do not have the type "array of constant char".

Attempting to modify a string literal in C produces undefined behavior
because the C standard specifically says so, not because, as you
incorrectly think, they are const qualified.

Yes. The problem was that I couldn't remember 'string literal'. I guess
I was wrong
trying to replace it with const string. Sorry, about that.

--
Ioan - Ciprian Tandau
tandau _at_ freeshell _dot_ org (hope it's not too late)
(... and that it still works...)

May 12 '06 #17
bo******@hotmail.com wrote:
I understand strtok seg faulting if the FIRST call is with NULL, but
in my case the first call is not NULL. It is
tokenptr = strtok(list, ";"); //this line causes the seg fault WHY?
list is a varible passed to the function.

You are passing a string literal as the list parameter, which you then
send to strtok(). This causes undefined behavior.

So?? I tried strtok(*list, ";"); and it still seg faults. According
to my old C book strtok is supposed to take a string for the first
argument. I even tried changing createvarible to:
int createvarible(const int sock, char *varname, char *vartype, char
*initialvalue, char accesslist[64])
and I still get a seg fault. How can I get this thing to work.

Think:

char *strtok(char *s1, const char *s2);

The string [s1] passed to strtok() must be mutable, because it is a
destructive function. Your appear to be passing the function a static
buffer or literal string.

Slow down a bit and re-read all the comments posted so far.

Consider:

[...]
char *fnord = "Fnord Motor Company";
fnord[0] = 'f'; /* Undefined behaviour */
[...]

Can you legally modify fnord, even with a direct assignment? If not,
what about a library function like strtok()?
May 12 '06 #18

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

40
by: Fatih Gey | last post by:
Hi, .. following causes a segfault. .. didn't know why ?! int main() { char name; strcpy (name, "ab8bc8cd8ed"); char cur;
4
by: collinm | last post by:
hi this is my code to analyse a file void analyzeFilename() { char string="B_L2_HLD_GRN_NOR_Run_Counter.txt"; char *tokenptr; char *seperators="_";
18
by: Robbie Hatley | last post by:
A couple of days ago I dedecided to force myself to really learn exactly what "strtok" does, and how to use it. I figured I'd just look it up in some book and that would be that. I figured...
3
by: nomad5000 | last post by:
Hi everybody! I'm having trouble using strtok to fill a matrix with int nrs. from a file. the code that is not working is the following: #include <iostream> #include <fstream> #include...
29
by: Pietro Cerutti | last post by:
Hello, here I have a strange problem with a real simple strtok example. The program is as follows: ### BEGIN STRTOK ### #include <string.h> #include <stdio.h>
4
by: ohaqqi | last post by:
Hi everybody. I haven't programmed anything in about 8 years, I've read up a little bit on C and need to write a shell in C. I want to use strtok() to take an input from a user and parse it into the...
3
by: krista | last post by:
Hi, I am beginner of C++. When I try to read a file and put it into X and Y variables. I got that segmentation fault. the data file is data.txt: 4.4 6.8 3.2 -5.5 3.3 0.9
4
by: Ashit Vora | last post by:
Hi, I have a quick question... I have a char* which contains value place:zip I want to extract both place and zip into some variable say myPlace and myZip. I used following functions for...
8
by: Neel | last post by:
Hi friends, I 'm trying to extract values from a lines which are delimited by a space eg. content of string is: "Hello World" I use strtok to extract "Hello" and "World" the code I use is
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.