小编Wat*_*221的帖子

将 strtok 存储在数组中 ~ C

parseInput老师给了我这个职能。我无法让它在没有分段错误的情况下运行,我希望你们能帮助我。

我认为这与我的通过方式有关total_cmd_words,但我不确定。

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

int parseInput(char *input, char* splitWords[]){
      int wordInd = 0;
      splitWords[0] = strtok(input, " "); // VSCODE says this line is causing the fault
      while(splitWords[wordInd] != NULL){
              splitWords[++wordInd] = strtok(NULL, " ");
      }

      return wordInd;
}

int main(){

  char* total_cmd = "file1 > file2";
  char* total_cmd_words[] = {};

  parseInput(total_cmd, total_cmd_words);

}
Run Code Online (Sandbox Code Playgroud)

gdb 给出以下输出:

__GI___strtok_r (s=0x555555556006 "file1 > file2", delim=0x555555556004 " ", 
    save_ptr=0x7ffff7fb0ca8 <olds>) at strtok_r.c:72
72  strtok_r.c: No such file …
Run Code Online (Sandbox Code Playgroud)

c strtok segmentation-fault

2
推荐指数
1
解决办法
89
查看次数

标签 统计

c ×1

segmentation-fault ×1

strtok ×1