details in my previous Q?????

B

batee5a

well.....i did understand your replies partially.. but what happened
to me is that when i had used the function (strtok)
from the library <cstring>...i sent it a char* beside the delimiter
"."...as the following example
char * sentence="hai.bai.rasha";
cout<<strtok(sentence,".");
then an application error in a dialog box appeared....
as i know and as i read before...the (strtok)function do recieve a
char* and a const char* as arguments.....so why that erroe did
appear??and when i tried with an array instead ,it worked fine.....
thanx
 
D

David Rubin

batee5a said:
well.....i did understand your replies partially.. but what happened
to me is that when i had used the function (strtok)
from the library <cstring>...i sent it a char* beside the delimiter
"."...as the following example
char * sentence="hai.bai.rasha";
cout<<strtok(sentence,".");

This produces undefined behavior because string literals are
non-mutable, and std::strtok attempts to change the contents of the
string argument (it adds NUL chars).
then an application error in a dialog box appeared....
as i know and as i read before...the (strtok)function do recieve a
char* and a const char* as arguments.....so why that erroe did
appear??and when i tried with an array instead ,it worked fine.....

Using an array allows the contents to be changed:

char sentence[] = "hai.bai.rahsa";

Note, however, that subsequent calls to std::strtok on the same logical
string should be made with a null source argument:

cout << std::strtok(0, ".");

See the recent thread "string tokenizing" for variations on string
tokenizing implementations:

http://groups.google.com/[email protected]&rnum=1

/david
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,145
Messages
2,570,825
Members
47,371
Latest member
Brkaa

Latest Threads

Top