You are here:

C++/strtok function of string.h


we have to use NULL as a first argument in strtok function after first call. but second call onwards we are not specifying the name of the input string. so if i am applying strtok on two strings then how compiler will determine that a second call to strtok is for which string.

You do not. You have to ensure strtok is working on one and _only_ one token string at a time. This is because strtok (like some other C library functions) makes use of internal static state. This also implies that strtok is _not_ thread safe.

Any call to strtok where the first token string parameter is _not_ a null pointer re-initialises the internal state to tokenise the new string. Thus as far as strtok is concerned, the previous set of tokenisation operations are done with and this is a new set of tokenisation requests.

Thus the easiest way to use strtok is to process all the tokens in each string in sequence. Complete tokenising of one string, move on to the next string.

The only other way I can see off the top of my head is to maintain the state of where strtok has processed up to for each string you are tokenising and pass this pointer back to strtok for each call along with the associated delimiter string. Without a bit more thought and checking I cannot say if this technique would work in all cases or if you would have to do a bit of the work strtok does for you.

Note also that by using the name string.h you imply you are using C not C++. Since 1998 (when the original version of the ISO C++ standard was published) C-library headers used from C++ have the C header name less the .h extension and prefixed with c, thus the C header <string.h> is the C++ header <cstring>.

If you are using C++, then there are some better options. The most obvious would be to replace strtok with some other facility - a class maybe which held the state in each object rather than globally so that you could use many StrTok class objects simultaneously.

If you are using a modern C++ compiler (MS VC8 (2005), g++ 4, etc) then you could use libraries such as those provided by Boost ( See the documentation ( for their string and text processing libraries. In particular check out the tokenizer library - this seems it is like the StrTok class I mused about above but more generalised. Also of interest might be the Boost regex (regular expression), spirit parser and xpressive libraries depending on what you are trying to do.

See the Boost homepage for a list of supported compilers for the current release of the libraries (1.34.1 at the time of writing). Note that not all libraries are supported or fully supported by all compilers, especially the older compilers (i.e. those with lower version numbers). There are tables of regression tests for platforms, compilers, and libraries - see the options under Regression Tests on the Boost homepage.

Hope this has been of some help.  


All Answers

Answers by Expert:

Ask Experts


Ralph McArdell


I am a software developer with more than 15 years C++ experience and over 25 years experience developing a wide variety of applications for Windows NT/2000/XP, UNIX, Linux and other platforms. I can help with basic to advanced C++, C (although I do not write just-C much if at all these days so maybe ask in the C section about purely C matters), software development and many platform specific and system development problems.


My career started in the mid 1980s working as a batch process operator for the now defunct Inner London Education Authority, working on Prime mini computers. I then moved into the role of Programmer / Analyst, also on the Primes, then into technical support and finally into the micro computing section, using a variety of 16 and 8 bit machines. Following the demise of the ILEA I worked for a small company, now gone, called Hodos. I worked on a part task train simulator using C and the Intel DVI (Digital Video Interactive) - the hardware based predecessor to Indeo. Other projects included a CGI based train simulator (different goals to the first), and various other projects in C and Visual Basic (er, version 1 that is). When Hodos went into receivership I went freelance and finally managed to start working in C++. I initially had contracts working on train simulators (surprise) and multimedia - I worked on many of the Dorling Kindersley CD-ROM titles and wrote the screensaver games for the Wallace and Gromit Cracking Animator CD. My more recent contracts have been more traditionally IT based, working predominately in C++ on MS Windows NT, 2000. XP, Linux and UN*X. These projects have had wide ranging additional skill sets including system analysis and design, databases and SQL in various guises, C#, client server and remoting, cross porting applications between platforms and various client development processes. I have an interest in the development of the C++ core language and libraries and try to keep up with at least some of the papers on the ISO C++ Standard Committee site at


©2017 All rights reserved.