将 strtok 存储在数组中 ~ C

发布于 2025-01-10 06:21:36 字数 990 浏览 0 评论 0原文

老师给了我 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);

}

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 or directory.

更改: char*total_cmd_words[] = {}; 对此: char*total_cmd_words[100] = {}; 仍然会导致分段错误。

I was given the parseInput function from a teacher. I am not able to get it to run without a segmentation fault and I hope you guys can help me.

I think it has to do with how I pass total_cmd_words, but I'm not sure.

// 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);

}

gdb gives this output:

__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 or directory.

Changing:
char* total_cmd_words[] = {};
to this:
char* total_cmd_words[100] = {};
Still results in a segmentation fault.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

欢烬 2025-01-17 06:21:36

您的 main 函数中有两个错误。

首先,您的 total_cmd_words 声明是错误的:就目前情况而言,您声明了一个长度为零的数组 - 因此没有空间来实际存储 strtok 返回的指针> 函数在您的 parseInput 函数中。您需要为 [] 内的数组指定一个维度 - 该维度足够大以容纳您可能遇到的最大数量的值。

其次,调用 strtok 修改作为第一个参数给出的字符串。但是,您的 total_cmd 是一个指向不可变字符串文字的指针。相反,请将其声明为 char数组,并使用字符串文字的副本对其进行初始化。

这是 main 的可能工作版本:

int main()
{
    char total_cmd[] = "file1 > file2";
    char* total_cmd_words[10] = {0,};
    int p = parseInput(total_cmd, total_cmd_words);
    printf("%d\n", p);
}

There are two errors in your main function.

First, your declaration of total_cmd_words is wrong: as it stands, you're declaring an array of zero length – so there is no space to actually store the pointrs returned by the strtok function in your parseInput function. You need to specify a dimension for the array inside the [] – one that is large enough to hold the maximum number of values that your are likely to encounter.

Second, a call to strtok modifies the string given as its first argument. However, your total_cmd is a pointer to a non-mutable string literal. Instead, declare that as an array of char and initialize it with a copy of the string literal.

Here's a possible working version of your main:

int main()
{
    char total_cmd[] = "file1 > file2";
    char* total_cmd_words[10] = {0,};
    int p = parseInput(total_cmd, total_cmd_words);
    printf("%d\n", p);
}
云雾 2025-01-17 06:21:36

当您声明时

char* total_cmd_words[] = {};

没有指定数组长度。因此,它的长度是由你的初始化器决定的,它是空的。

因此,当您这样做时,

splitWords[0] = strtok(input, " ");

您将分配给 0 长度数组的第一个元素。这是未定义的行为,也是导致分段错误的最可能原因。

When you declared

char* total_cmd_words[] = {};

you didn't specify an array length. Therefore, it's length was determined by your initializer which was empty.

So, when you do

splitWords[0] = strtok(input, " ");

you're assigning to the first element of a 0-length array. That's undefined behavior and the most likely cause of your segmentation fault.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文