C - Unexpected Segmentation Fault on strtok(…)

╄→尐↘猪︶ㄣ 提交于 2019-12-01 00:42:24

you are first checking if token is not equal to NULL(when it is, it breaks out of the while loop). Then you are comparing token, which is a NULL with a constant NUMBER? here: strcmp(token, 0) when strcmp expects 2 strings, you provide a number. strcmp will try to fetch a string at 0th address(or NULL) giving you a segmentation fault.

while(strcmp(token, 0) != 0){
    token = strtok(NULL, ch);
    printf("%s\n",token);
  }

Also this piece of code should be something like the following:

change

  char * token = strtok(from, ch);
  printf("%s\n",token);
  while(token != NULL){
    token = strtok(NULL, ch);
    printf("%s\n", token);
  }

to

  char * token = strtok(from, ch);
  printf("%s\n",token);
  while(token != NULL){
    printf("%s\n", token);
    token = strtok(NULL, ch);
  }

strtok modifies its first argument. You are passing it a string from read-only memory, and the segfault occurs when strtok tries to change it. Try changing from:

char* from = "12.34.56.78";

to

char from[] = "12.34.56.78";

This is a problem:

  while(token != NULL){
    token = strtok(NULL, ch);
    printf("%s\n", token);
  }

You're checking for NULL, but then calling strtok again and not checking after that but before printing.

There are other problems with the code, but I suspect this is why it crashes where it does now.

The problem is that even though you terminate the loop when strtok() returns NULL, you try to print the NULL first:

  while(token != NULL){
    token = strtok(NULL, ch);
    printf("%s\n", token);    // not good when token is NULL
  }

It turns out there are several opportunities in addition to this one for segfaults in this example, as pointed out by other answers.

Here's one way to handle your example tokenization:

char from[] = "12.34.56.78";
char * ch = ".";
char * token = strtok(from, ch);
while (token != NULL){
    printf("%s\n", token);
    token = strtok(NULL, ch);
}

If purpose of code is only to print element separated by '.', Only change in char declaration and before printing token check for its value NULL or not !

 main(){
        char from[] = "12.34.56.78.100.101";
        char * ch = ".";
        char * token = strtok(from, ch);
        //printf("%s\n",token);
        while(token != NULL){
            printf("%s\n", token);
            token = strtok(NULL, ch);
        }
   }
OUTPUT
  ./test1
 12
 12
 34
 56
 78
 100
 101

You have both memory access errors and logic errors. I will only address the memory access errors that are causing your program to crash.

strtok modifies it's first argument. Since you are passing in a string literal, it is unable to modify the string (string literals are not modifiable.)

Here's a possible fix to define from as a modifiable string array:

char from[] = "12.34.56.78";

Because strtok modifies the string passed into it, you cannot process that string again in your second while loop. You are essentially passing in a NULL into the strcmp function there. A possible fix would be to copy the from array into another buffer each time you wish to use strtok.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!