473,382 Members | 1,717 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,382 software developers and data experts.

Why occur the mistake in runtime about strtok()?

hu
hi, everybody!
I'm testing the fuction of strtok(). The environment is WinXP, VC++6.0.
Program is simple, but mistake is confusing. First, the below code can get
right outcome:"ello world, hello dreams."

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

int main()
{
char *pStr = "Hello world, hello dreams.";
char *p = pStr;
p = strtok(pStr, "H");
if (NULL==p)
printf("null__");
else
printf("%s", p);

return (0);
}

Second, Changing the sentence:
p = strtok(pStr, "H");
to
p = strtok(pStr, "Ho");
It can be compiled, but cannot run!

Third, Changing the sentence:
p = strtok(pStr, "H");
to
p = strtok(pStr, "e");
It can be compiled, but cannot run too!

Where is the wrong?
thanks!
Jun 18 '06 #1
8 1902
hu wrote:
hi, everybody!
I'm testing the fuction of strtok(). The environment is WinXP, VC++6.0.
Program is simple, but mistake is confusing. First, the below code can get
right outcome:"ello world, hello dreams."

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

int main()
{
char *pStr = "Hello world, hello dreams.";
char *p = pStr;
p = strtok(pStr, "H");


This invokes undefined behaviour, you are calling strtok with a string
literal. strtok overwrites the string you pass it, doing this with a
literal is undefined.

--
Ian Collins.
Jun 18 '06 #2
"hu" <hu******@gmail.com> writes:
I'm testing the fuction of strtok(). The environment is WinXP, VC++6.0.
Program is simple, but mistake is confusing. First, the below code can get
right outcome:"ello world, hello dreams."

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

int main()
{
char *pStr = "Hello world, hello dreams.";
char *p = pStr;
p = strtok(pStr, "H");
if (NULL==p)
printf("null__");
else
printf("%s", p);

return (0);
}

Second, Changing the sentence:
p = strtok(pStr, "H");
to
p = strtok(pStr, "Ho");
It can be compiled, but cannot run!

Third, Changing the sentence:
p = strtok(pStr, "H");
to
p = strtok(pStr, "e");
It can be compiled, but cannot run too!

Where is the wrong?


Three things. First, you're using a non-standard header <stdafx.h>.
Second, you're trying to modify a string literal (that's your real
problem). Third, you're you're not telling us how it fails; "cannot
run" wouldn't have given us much information if the actual problem
didn't happen to be obvious.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jun 18 '06 #3
hu
the strtok() declaration:
char * strtok(char *p1, const char*p2);
The strtok() can overwrite *p1? And what it overwirte the content of p1
with?

thanks!

"Ian Collins" <ia******@hotmail.com>
??????:4f*************@individual.net...
hu wrote:
hi, everybody!
I'm testing the fuction of strtok(). The environment is WinXP,
VC++6.0.
Program is simple, but mistake is confusing. First, the below code can
get
right outcome:"ello world, hello dreams."

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

int main()
{
char *pStr = "Hello world, hello dreams.";
char *p = pStr;
p = strtok(pStr, "H");


This invokes undefined behaviour, you are calling strtok with a string
literal. strtok overwrites the string you pass it, doing this with a
literal is undefined.

--
Ian Collins.

Jun 19 '06 #4
hu wrote:

the strtok() declaration:
char * strtok(char *p1, const char*p2);
The strtok() can overwrite *p1?
And what it overwirte the content of p1 with?


You seem to have no idea of what strtok does.

N869
7.21.5.8 The strtok function
Synopsis
[#1]
#include <string.h>
char *strtok(char * restrict s1,
const char * restrict s2);
Description
[#2] A sequence of calls to the strtok function breaks the
string pointed to by s1 into a sequence of tokens, each of
which is delimited by a character from the string pointed to
by s2. The first call in the sequence has a non-null first
argument; subsequent calls in the sequence have a null first
argument. The separator string pointed to by s2 may be
different from call to call.
[#3] The first call in the sequence searches the string
pointed to by s1 for the first character that is not
contained in the current separator string pointed to by s2.
If no such character is found, then there are no tokens in
the string pointed to by s1 and the strtok function returns
a null pointer. If such a character is found, it is the
start of the first token.
[#4] The strtok function then searches from there for a
character that is contained in the current separator string.
If no such character is found, the current token extends to
the end of the string pointed to by s1, and subsequent
searches for a token will return a null pointer. If such a
character is found, it is overwritten by a null character,
which terminates the current token. The strtok function
saves a pointer to the following character, from which the
next search for a token will start.
[#5] Each subsequent call, with a null pointer as the value
of the first argument, starts searching from the saved
pointer and behaves as described above.
[#6] The implementation shall behave as if no library
function calls the strtok function.
Returns
[#7] The strtok function returns a pointer to the first
character of a token, or a null pointer if there is no
token.
[#8] EXAMPLE 1
#include <string.h>
static char str[] = "?a???b,,,#c";
char *t;
t = strtok(str, "?"); // t points to the token "a"
t = strtok(NULL, ","); // t points to the token "??b"
t = strtok(NULL, "#,"); // t points to the token "c"
t = strtok(NULL, "?"); // t is a null pointer
--
pete
Jun 19 '06 #5
hu
I said "can not run", I mean that when I start the program, WindowsXP pop a
illegal operation dialog box.

thanks a lot.
"Keith Thompson" <ks***@mib.org> ??????:ln************@nuthaus.mib.org...
"hu" <hu******@gmail.com> writes:
I'm testing the fuction of strtok(). The environment is WinXP,
VC++6.0.
Program is simple, but mistake is confusing. First, the below code can
get
right outcome:"ello world, hello dreams."

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

int main()
{
char *pStr = "Hello world, hello dreams.";
char *p = pStr;
p = strtok(pStr, "H");
if (NULL==p)
printf("null__");
else
printf("%s", p);

return (0);
}

Second, Changing the sentence:
p = strtok(pStr, "H");
to
p = strtok(pStr, "Ho");
It can be compiled, but cannot run!

Third, Changing the sentence:
p = strtok(pStr, "H");
to
p = strtok(pStr, "e");
It can be compiled, but cannot run too!

Where is the wrong?


Three things. First, you're using a non-standard header <stdafx.h>.
Second, you're trying to modify a string literal (that's your real
problem). Third, you're you're not telling us how it fails; "cannot
run" wouldn't have given us much information if the actual problem
didn't happen to be obvious.

--
Keith Thompson (The_Other_Keith) ks***@mib.org
<http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*>
<http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.

Jun 19 '06 #6
hu wrote:
I said "can not run", I mean that when I start the program, WindowsXP pop a
illegal operation dialog box.


(a) Please don't top-post.

(b) So, by "can not run", you mean "did run, and broke", or just "crashed".

--
Chris "don't attempt to modify string literals" Dollin
"Reaching out for mirrors hidden in the web." - Renaissance, /Running Hard/

Jun 19 '06 #7

hu wrote:
hi, everybody!
I'm testing the fuction of strtok(). The environment is WinXP, VC++6.0.
Program is simple, but mistake is confusing. First, the below code can get
right outcome:"ello world, hello dreams."

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

int main()
{
char *pStr = "Hello world, hello dreams.";


This is your problem. The strtok() function has to be able to modify
the string being tokenized; it will overwrite each delimiter it finds
with the nul character. Unfortunately, string literals may not be
writable, so attempting to write to a string literal may invoke
undefined behavior. The results of invoking undefined behavior may
include running as expected, crashing outright, or something more
subtle and difficult to debug.

The easiest way to fix the problem is to change the above declaration
to

char pStr[] = "Hello world, hello dreams.";

Instead of creating a pointer to a non-writable string literal, you've
created an array that can be written to, and the array contents are
initialized with a copy of the string literal.

Grab your handy C reference manual and read up on string literals,
arrays, and strtok().

[snip remainder]

Jun 19 '06 #8
hu wrote:

I said "can not run", I mean that when I start the program,
WindowsXP pop a illegal operation dialog box.


Please do not top-post, especially in technical newsgroups. Your
answer belongs after (or possibly intermixed with) the quoted (and
snipped) material to which you reply.

See the following links for further information

--
Some informative links:
news:news.announce.newusers
http://www.geocities.com/nnqweb/
http://www.catb.org/~esr/faqs/smart-questions.html
http://www.caliburn.nl/topposting.html
http://www.netmeister.org/news/learn2quote.html
Jun 19 '06 #9

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

Similar topics

2
by: Ram Laxman | last post by:
Hi all, I have written the following code: /* strtok example */ #include <stdio.h> #include <string.h> static const char * const resultFileName = "param.txt";
6
by: gyan | last post by:
Hi How strtok track through string? char *strtok(char *s1, const char *s2); As i know The first call (with pointer s1 specified) returns a pointer to the first character of the first...
13
by: ern | last post by:
I'm using strtok( ) to capture lines of input. After I call "splitCommand", I call strtok( ) again to get the next line. Strtok( ) returns NULL (but there is more in the file...). That didn't...
20
by: bubunia2000 | last post by:
Hi all, I heard that strtok is not thread safe. So I want to write a sample program which will tokenize string without using strtok. Can I get a sample source code for the same. For exp:...
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>
20
by: JohnQ | last post by:
The way I understand the startup of a C++ program is: A.) The stuff that happens before the entry point. B.) The stuff that happens between the entry point and the calling of main(). C.)...
11
by: magicman | last post by:
can anyone point me out to its implementation in C before I roll my own. thx
12
by: Pilcrow | last post by:
Here is a quick program, together with its output, that illustrates what I consider to be a deficiency of the standard function strtok from <string.h>: I am using C:\>gcc --version gcc (GCC)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: 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?
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.