473,785 Members | 2,209 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1930
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_Keit h) 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******@hotma il.com>
??????:4f****** *******@individ ual.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.or g> ??????: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_Keit h) 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.annou nce.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
414
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
476
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 token, and will have written a null character into s1 immediately following the returned token. The function keeps track of its position in the string between separate calls, so that subsequent calls (which must be made with the first argument...
13
4927
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 happen before 'splitCommands' entered the picture. The problem is in splitCommands( ) somehow modifying the pointer, but I HAVE to call that function. Is there a way to make a copy of it or something ? /* HERE IS MY CODE */ char *...
20
17243
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: 0.0.0.0--->I want to tokenize the string using delimiter as as dot. Regards
3
3811
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 <string> #include <stdlib.h> using namespace std;
29
2587
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
6099
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.) main(). So, if the above is OK, does static initialization occur during A or B? What happens during A?
11
17178
by: magicman | last post by:
can anyone point me out to its implementation in C before I roll my own. thx
12
2360
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) 3.4.5 (mingw special) I would like there to be a default, to be returned when two delimiters
0
9646
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
10096
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9956
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7504
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6742
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5386
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4055
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3658
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2887
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.