1

I am trying to split a char array with delimiter.

This gives me a runtime error:

#include <iostream>
#include <cstring>
int main()
{
    char* largechars = "q=test&use=bingo";
    char* chars_array = strtok(largechars, "&");
    while(chars_array)
    {
        std::cout << chars_array << '\n';
        chars_array = strtok(NULL, "&");
    }
}

Demo here http://ideone.com/OpNssn

This program works fine:

#include <iostream>
#include <cstring>
int main()
{
    char largechars[] = "q=test&use=bingo";
    char* chars_array = strtok(largechars, "&");
    while(chars_array)
    {
        std::cout << chars_array << '\n';
        chars_array = strtok(NULL, "&");
    }
}

Demo here http://ideone.com/Ye8C8k

What is the issue here?

default
  • 11,485
  • 9
  • 66
  • 102
Kathick
  • 1,395
  • 5
  • 19
  • 30
  • Using strtok in C++ should give you a hint that something might be wrong. Also, literals are const char *. – Bartek Banachewicz Mar 08 '13 at 07:52
  • but both are char array am i correct? – Kathick Mar 08 '13 at 07:54
  • 2
    No, the first version isn't using an array, but a pointer to a string literal. Literals are constants. And [pointers and arrays are *very* different](http://stackoverflow.com/questions/9460260/what-is-the-difference-between-char-a-string-and-char-p-string). – Bo Persson Mar 08 '13 at 07:55
  • About splitting strings in C++, check out [this question](http://stackoverflow.com/questions/236129/splitting-a-string-in-c). – Björn Pollex Mar 08 '13 at 07:56
  • Closing & Read about differences between pointers and arrays. – Bartek Banachewicz Mar 08 '13 at 07:56
  • @BartekBanachewicz Here http://www.cplusplus.com/reference/cstring/strtok/ the functions take char pointer as parameter so i thought i could use it. – Kathick Mar 08 '13 at 07:58
  • @Kathick - You're on the right track, but a pointer to a literal is `const char*` and not what `strtok` expects. The compiler should warn you that the first assignment is a bit fishy. – Bo Persson Mar 08 '13 at 08:05
  • Don't use C functions. Don't use cplusplus.com. An *read* before asking questions. – Bartek Banachewicz Mar 08 '13 at 08:05
  • @Kathick A string literal has type `char const[]` in C++, and in C, it is defined to be immutable despite having a type `char[]`. And of course, `strtok` is one of those functions which are broken, even in C, and should never be used. – James Kanze Mar 08 '13 at 09:01

2 Answers2

0

strtok() modifies its argument when it parses the string

Your first example supplies a variable that is read-only as argument to the strtok and therefore you get the runtime error. In your second example you supply an argument that can be modified.

AndersK
  • 35,813
  • 6
  • 60
  • 86
0

the contents of string argumnet (in your case largechars) is modified and broken into smaller strings (tokens) by this function strtok. But in your first sample first argument is const char* So when it tries to modify the content it will result in run time error

999k
  • 6,257
  • 2
  • 29
  • 32