tags:

views:

433

answers:

8

Can anyone explain why I am getting segmentation fault in the following example?

#include <stdio.h>
#include <string.h>

int main(void) {
  char *hello = "Hello World, Let me live.";
  char *tokens[50];
  strtok_r(hello, " ,", tokens);
  int i = 0;
  while(i < 5) {
    printf("%s\n", tokens[i++]);
  }
}
A: 

I think it might be the char *tokens[50]; because you are declaring it a pointer when it is already a pointer. An array is already a pointer upon declaration. You mean to say char tokens[50];. That should do the trick.

SapphireSun
Your understanding of C seems even weaker than me...
Phulore R - Profile 2
A: 

try this:

  char tokens[50];
  strtok_r(hello, " ,", &tokens);

notice tokens is no longer a char* and tokens uses the '&'

0xC0DEFACE
+3  A: 

strtok_r tries to write null characters into hello (which is illegal because it is a const string)

Ryan L Brown
I tried `char hello[50]` . The segmentation fault is vanished but now the problem is `printf` just prints sad blank lines. :(
Phulore R - Profile 2
A: 

If I recall correctly, strtok/strtok_r modifies the input string and it is not right to pass string literals to it.

Moron
Can you please elaborate? I am a real moron when it comes to coding in C...
Phulore R - Profile 2
String literals are read only (usually, depends on the compiler though). strtok_r modifies the input. Trying to modify a read only string will cause a segfault. So passing a string literal to strtok_r will cause a segfault.
Moron
A: 

You have understood the usage of strtok_r incorrectly. Please check this example and documentation

And try & see this:

#include <stdio.h>
#include <string.h>    

int main(void)
{
    char hello[] = "Hello World, let me live.";

    char *tmp;
    char *token = NULL;
    for(token = strtok_r(hello, ", ", &tmp);
        token != NULL;
        token = strtok_r(NULL, ", ", &tmp))
    {
        printf("%s\n", token);
    }

    return 0;
}
Anssi
+4  A: 

Try this:

#include <stdio.h>
#include <string.h>

int main(void) {
        char hello[] = "Hello World, Let me live."; // make this a char array not a pointer to literal.
        char *rest; // to point to the rest of the string after token extraction.
        char *token; // to point to the actual token returned.
        char *ptr = hello; // make q point to start of hello.

        // loop till strtok_r returns NULL.
        while(token = strtok_r(ptr, " ,", &rest)) {

                printf("%s\n", token); // print the token returned.
                ptr = rest; // rest contains the left over part..assign it to ptr...and start tokenizing again.    
        }
}
/*
Output:
Hello
World
Let
me
live.
*/
codaddict
Thanks a tonne. :)
Phulore R - Profile 2
+2  A: 

A bunch of things wrong:

  1. hello points to a string literal, which must be treated as immutable. (It could live in read-only memory.) Since strtok_r mutates its argument string, you can't use hello with it.

  2. You call strtok_r only once and don't initialize your tokens array to point to anything.

Try this:

#include <stdio.h>
#include <string.h>

int main(void) {
  char hello[] = "Hello World, Let me live.";
  char *p = hello;
  char *tokens[50];
  int i = 0;

  while (i < 50) {
     tokens[i] = strtok_r(p, " ,", &p);
     if (tokens[i] == NULL) {
        break;
     }
     i++;
  }

  i = 0;
  while (i < 5) {
    printf("%s\n", tokens[i++]);
  }

  return 0;
}
jamesdlin
Thanks for the answer. I wish SO allowed marking multiple answers as correct. :)
Phulore R - Profile 2
+3  A: 
  • You need to call strtok_r in a loop. The first time you give it the string to be tokenized, then you give it NULL as the first parameter.
  • strtok_r takes a char ** as the third parameter. tokens is an array of 50 char * values. When you pass tokens to strtok_r(), what gets passed is a char ** value that points to the first element of that array. This is okay, but you are wasting 49 of the values that are not used at all. You should have char *last; and use &last as the third parameter to strtok_r().
  • strtok_r() modifies its first argument, so you can't pass it something that can't be modified. String literals in C are read-only, so you need something that can be modified: char hello[] = "Hello World, Let me live."; for example.
Alok
Thanks for the answer. I wish SO allowed marking multiple answers as correct. :)
Phulore R - Profile 2
+1, Best answer.
Tim Post
@Scrub: glad to be of help. Make sure you understand my second point above (about `char *tokens[50];` being equivalent to `char **` when passed to a function).
Alok