0

The following code returns a segmentation fault and I don't get why. This is pretty much as the most examples I found online, so what's going on?

#include <stdio.h>                                                                                                                                                                                                                           
#include <string.h>                                                                                                                                                                                                                          
                                                                                                                                                                                                                                             
int main(int argc, char * argv) {                                                                                                                                                                                                            
  char * mystring = "a b c";                                                                                                                                                                                                                 
  char* p = strtok(mystring, " ");                                                                                                                                                                                                           
  while(p != NULL) {                                                                                                                                                                                                                         
    printf("%s\n", p);                                                                                                                                                                                                                       
    p = strtok(NULL, " ");                                                                                                                                                                                                                   
  }                                                                                                                                                                                                                                          
  return 0;                                                                                                                                                                                                                                  
}   

I compiled the go with cc -g tokenize.c -o mytokenize. I tried to do a bit of debugging. The only thing I can say for sure is that is failing on strtok(mystring, " ");. Can someone tell me what I am doing wrong?

TBA
  • 1,921
  • 4
  • 13
  • 26
b10n1k
  • 567
  • 5
  • 21
  • 2
    `strtok` modifies the string you give it. String literals cannot be modified. – rici May 29 '22 at 07:08
  • i am not sure i get it. What makes "a b c" literal? what is the different with the example from https://www.tutorialspoint.com/c_standard_library/c_function_strtok.htm which just work – b10n1k May 29 '22 at 15:12
  • 1
    `"a b c"` is a string literal by definition, just like 5 is an integer literal and 3.14 is a floating point literal. In C, string literals correspond to *immutable* (unmodifiable) arrays of characters and the string literal is replaced with a pointer to that array. So `char* m = "a b c";` makes `m` a pointer to an unmodifiable array. `char m2[] = "a b c";` makes `m2` a new array, which is initialised to a *copy* of the characters in the string literal. So `m2` can be modified but `m` cannot be. Consult any good textbook on C for a longer explanation. – rici May 29 '22 at 15:29

0 Answers0