分段错误 - Strtok - Linux C
Segmentation fault - Strtok - Linux C
我被这个"error"蒙蔽了双眼。该函数将命令及其参数与字符串分开。我正在为此使用 strtok。我很确定这是愚蠢的事情,但我看不到它。
函数为:
int dataCommand(char command[], char *data[]){
char *ch;
printf("Split \"%s\"\n", command);
ch = strtok(command, "_");
int i = 0;
data[i] = ch;
printf("%s\n", data[i]);
while (ch != NULL) {
ch = strtok(NULL, "_");
data[++i] = ch;
printf("Valor ch Salida: %s\n", ch);
}
printf("dataCommand END");
return 0;
}
这个函数的调用是:
char *data[MAX_PARAM]; //MAX_PARAM = 80
char command[] ="UMBR_Donostia_1_2";
dataCommand(command,data);
屏幕上的结果:
Split "UMBR_Donostia_1_2"
UMBR
Valor ch Salida: Donostia
Valor ch Salida: 1
Valor ch Salida: 2
Segmentation fault (core dumped)
我一直在调查,我发现的大多数错误是在 strtok 上使用 char *,所以他们使用的是文字,但我使用的是 char[]。我不知道还能是什么。谢谢
在循环中调用 strtok()
在 处理完当前令牌之后。
while (ch != NULL) {
printf("Valor ch Salida: %s\n", ch);
data[++i] = ch;
ch = strtok(NULL, "_");
}
由于您首先调用了 strtok()
,因此您跳过了第一个标记,当您到达结尾时,您正在使用空指针调用 printf()
。
我的编译器对你的程序的结果:
Split "UMBR_Donostia_1_2"
UMBR
Valor ch Salida: Donostia
Valor ch Salida: 1
Valor ch Salida: 2
Valor ch Salida: (null)
很明显,您向它传递了一个空值。
[UPD1]
#include <stdio.h>
#include <string.h>
#define MAX_PARAM 80
int dataCommand(char command[], char *data[]){
char *ch;
printf("Split \"%s\"\n", command);
int i = 0;
ch = strtok(command, "_");
while (ch != NULL)
{
data[i++] = ch;
printf("Valor ch Salida: %s\n", ch);
ch = strtok(NULL, "_");
}
printf("dataCommand END");
return 0;
}
int main()
{
char *data[MAX_PARAM]; //MAX_PARAM = 80
char command[] ="UMBR_Donostia_1_2";
dataCommand(command,data);
return 0;
}
在你的循环中,你正在调用 strtok
来获取下一个标记,但你在对它做任何事情之前没有检查它是否为 NULL。
重新格式化以将 strtok
放在循环的末尾,如下所示:
int dataCommand(char command[], char *data[]){
char *ch;
printf("Split \"%s\"\n", command);
int i = 0;
ch = strtok(command, "_");
while (ch != NULL) {
data[i++] = ch;
printf("Valor ch Salida: %s\n", ch);
ch = strtok(NULL, "_");
}
printf("dataCommand END");
return 0;
}
另请注意,循环之前的一些冗余代码已被删除,以支持循环中的代码。
试试这个:
int
dataCommand(char *command,char *data[])
{
char *ch;
int i = 0;
while (1) {
ch = strtok(command, "_");
command = NULL;
if (ch == NULL)
break;
data[i++] = ch;
printf("Valor ch Salida: %s\n", ch);
}
printf("dataCommand END\n");
return 0;
}
我被这个"error"蒙蔽了双眼。该函数将命令及其参数与字符串分开。我正在为此使用 strtok。我很确定这是愚蠢的事情,但我看不到它。 函数为:
int dataCommand(char command[], char *data[]){
char *ch;
printf("Split \"%s\"\n", command);
ch = strtok(command, "_");
int i = 0;
data[i] = ch;
printf("%s\n", data[i]);
while (ch != NULL) {
ch = strtok(NULL, "_");
data[++i] = ch;
printf("Valor ch Salida: %s\n", ch);
}
printf("dataCommand END");
return 0;
}
这个函数的调用是:
char *data[MAX_PARAM]; //MAX_PARAM = 80
char command[] ="UMBR_Donostia_1_2";
dataCommand(command,data);
屏幕上的结果:
Split "UMBR_Donostia_1_2"
UMBR
Valor ch Salida: Donostia
Valor ch Salida: 1
Valor ch Salida: 2
Segmentation fault (core dumped)
我一直在调查,我发现的大多数错误是在 strtok 上使用 char *,所以他们使用的是文字,但我使用的是 char[]。我不知道还能是什么。谢谢
在循环中调用 strtok()
在 处理完当前令牌之后。
while (ch != NULL) {
printf("Valor ch Salida: %s\n", ch);
data[++i] = ch;
ch = strtok(NULL, "_");
}
由于您首先调用了 strtok()
,因此您跳过了第一个标记,当您到达结尾时,您正在使用空指针调用 printf()
。
我的编译器对你的程序的结果:
Split "UMBR_Donostia_1_2"
UMBR
Valor ch Salida: Donostia
Valor ch Salida: 1
Valor ch Salida: 2
Valor ch Salida: (null)
很明显,您向它传递了一个空值。
[UPD1]
#include <stdio.h>
#include <string.h>
#define MAX_PARAM 80
int dataCommand(char command[], char *data[]){
char *ch;
printf("Split \"%s\"\n", command);
int i = 0;
ch = strtok(command, "_");
while (ch != NULL)
{
data[i++] = ch;
printf("Valor ch Salida: %s\n", ch);
ch = strtok(NULL, "_");
}
printf("dataCommand END");
return 0;
}
int main()
{
char *data[MAX_PARAM]; //MAX_PARAM = 80
char command[] ="UMBR_Donostia_1_2";
dataCommand(command,data);
return 0;
}
在你的循环中,你正在调用 strtok
来获取下一个标记,但你在对它做任何事情之前没有检查它是否为 NULL。
重新格式化以将 strtok
放在循环的末尾,如下所示:
int dataCommand(char command[], char *data[]){
char *ch;
printf("Split \"%s\"\n", command);
int i = 0;
ch = strtok(command, "_");
while (ch != NULL) {
data[i++] = ch;
printf("Valor ch Salida: %s\n", ch);
ch = strtok(NULL, "_");
}
printf("dataCommand END");
return 0;
}
另请注意,循环之前的一些冗余代码已被删除,以支持循环中的代码。
试试这个:
int
dataCommand(char *command,char *data[])
{
char *ch;
int i = 0;
while (1) {
ch = strtok(command, "_");
command = NULL;
if (ch == NULL)
break;
data[i++] = ch;
printf("Valor ch Salida: %s\n", ch);
}
printf("dataCommand END\n");
return 0;
}