将 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);
}
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的
main
函数中有两个错误。首先,您的
total_cmd_words
声明是错误的:就目前情况而言,您声明了一个长度为零的数组 - 因此没有空间来实际存储strtok
返回的指针> 函数在您的parseInput
函数中。您需要为[]
内的数组指定一个维度 - 该维度足够大以容纳您可能遇到的最大数量的值。其次,调用
strtok
修改作为第一个参数给出的字符串。但是,您的total_cmd
是一个指向不可变字符串文字的指针。相反,请将其声明为char
的数组,并使用字符串文字的副本对其进行初始化。这是
main
的可能工作版本: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 thestrtok
function in yourparseInput
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, yourtotal_cmd
is a pointer to a non-mutable string literal. Instead, declare that as an array ofchar
and initialize it with a copy of the string literal.Here's a possible working version of your
main
:当您声明时
没有指定数组长度。因此,它的长度是由你的初始化器决定的,它是空的。
因此,当您这样做时,
您将分配给 0 长度数组的第一个元素。这是未定义的行为,也是导致分段错误的最可能原因。
When you declared
you didn't specify an array length. Therefore, it's length was determined by your initializer which was empty.
So, when you do
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.