Question

the program for strtok given on http://www.opengroup.org/onlinepubs/000095399/functions/strtok.html crashes everytime..

#include <string.h>
...
char *token;
char *line = "LINE TO BE SEPARATED";
char *search = " ";


/* Token will point to "LINE". */
token = strtok(line, search);


/* Token will point to "TO". */
token = strtok(NULL, search);

If I use a char array for variable 'line', it works. i.e. char line[] = "LINE TO BE SEPARATED" works.

Kindly explain.

Was it helpful?

Solution

strtok modifies the input string line.

char *line = "LINE TO BE SEPARATED";

In this case line points to the read-only memory. Hence, cannot be modified. You need to pass char array for strtok.

OTHER TIPS

Since this has a C++ tag:

// Beware, brain-compiled code ahead!
#include <string>
#include <sstream>
#include <iostream>

int main()
{
  std::istringstream iss("LINE TO BE SEPARATED");
  while( iss.good() ) {
    std::string token;
    iss >> token;
    std::cout << token '\n';
  }

  return 0;
}

Edit: As Konrad said in his comment, the above loop could be replaced by std::copy working on stream iterators:

// Beware, brain-compiled code ahead!
#include <string>
#include <sstream>
#include <iostream>
#include <algorithm>

int main()
{
  std::istringstream iss("LINE TO BE SEPARATED");
  std::copy( std::istream_iterator<string>(std::iss)
           , std::istream_iterator<string>()
           , std::ostream_iterator<string>(std::cout, "\n") );
  return 0;
}

I have to (grudgingly) admit that there's something to be said for it.

char *line is a pointer and you are pointing it to a constant string ("LINE TO BE SEPARATED"). This fails when strtok attempts to modify that string. It would be better to qualify this variable as const char *line—still wouldn't work, but might lead to a helpful warning when you try to pass it to strtok.

Meanwhile the array char line[] can be modified (it's not const) and is only initialised to contain the string.

aJ said what is needed. My advice is avoid that ugly & unsafe strtok. You are using C++ so go ahead with std::string. You also can use Boost http://www.boost.org/doc/libs/1_43_0/libs/libraries.htm#String & http://www.boost.org/doc/libs/1_43_0/doc/html/string_algo.html . If you want a new string class, you may look at http://bstring.sourceforge.net/ .

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top