C 标记字符串错误
我试图标记一些字符串,使数字本身就是标记,这样我最终可以添加它们,但我的标记化不起作用,我不知道为什么。它编译正确,但当我执行文件时,它会显示“分段错误”,有人知道为什么或如何将数字作为令牌吗C 标记字符串错误,c,segmentation-fault,token,C,Segmentation Fault,Token,我试图标记一些字符串,使数字本身就是标记,这样我最终可以添加它们,但我的标记化不起作用,我不知道为什么。它编译正确,但当我执行文件时,它会显示“分段错误”,有人知道为什么或如何将数字作为令牌吗 #include <stdio.h> #include <stdlib.h> #include <string.h> int stringSum(char *s); /*void minMaxValues(char *s, int *min, int *max);*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int stringSum(char *s);
/*void minMaxValues(char *s, int *min, int *max);*/
int main(void)
{
int value,i;
char* text[] = { "1 + 2 + 3 + 4",
"7",
"30 + 20 + 10",
"9 + 900 + 90000 + 9000 + 90" };
for(i=0;i<4;i++) /*send strings to function*/
stringSum(text[i]);
}
int stringSum(char *s)
{
char* del = " + ";
char* token;
token = strtok(s,del);
while(token != NULL)
{
printf("%s\n",token);
token = strtok(NULL, del);
}
}
#包括
#包括
#包括
int stringSum(字符*s);
/*void minMaxValues(char*s,int*min,int*max)*/
内部主(空)
{
int值,i;
字符*文本[]={“1+2+3+4”,
"7",
"30 + 20 + 10",
"9 + 900 + 90000 + 9000 + 90" };
对于(i=0;i函数strtok
修改原始字符串。尝试传递char s[]
而不是char*s
和
为什么我们要使用chars[]
从这里查看char指针
和char数组
之间的区别()
在strtok
函数的规范中,s
必须是可写的,但在您的示例中,它是一个字符串文本
char* text[] = { "1 + 2 + 3 + 4",
"7",
"30 + 20 + 10",
"9 + 900 + 90000 + 9000 + 90" };
text
数组中的所有字符串都是字符串文字,因此不可修改。strtok
需要可修改的字符串。您在main
中定义了不可修改的字符串文字数组,因此它们不能与strtok
一起使用
要快速解决问题,请使用:
char* temp = malloc(strlen(s) + 1);
strcpy(temp, s);
token = strtok(temp,del);
这将动态分配一个可修改字符串,该字符串的值与s
相同,您可以在strtok
中使用该字符串
由于这是一个动态分配的变量,请记住在方法末尾释放内存:
free(temp);
您修改的stringSum
方法现在应该如下所示:
int stringSum(char *s)
{
char* del = " + ";
char* token;
char* temp = malloc(strlen(s) + 1);
strcpy(temp, s);
token = strtok(temp,del);
while(token != NULL)
{
printf("%s\n",token);
token = strtok(NULL, del);
}
free(temp);
return 0; //or whatever it is that you want to return
}
更改strtok是非法的,第一个参数是不能更改的字符串文字
备选方案:
#include <stdio.h>
#include <stdlib.h>
int stringSum(const char *s);
int main(void){
int value, i, size;
char *text[] = { "1 + 2 + 3 + 4",
"7",
"30 + 20 + 10",
"9 + 900 + 90000 + 9000 + 90"
};
size = sizeof(text)/sizeof(*text);
for(i=0;i<size;i++)
stringSum(text[i]);
return 0;
}
int stringSum(const char *s){
int sum = 0;
char *endp;
do{
int num = strtol(s, &endp, 10);
if(*endp == ' ' || *endp == '+' || *endp == '\0'){
sum += num;
if(*endp)
s = ++endp;
} else { //s has invalid character
fprintf(stderr, "Illegal characters are included.\n");
break;
}
}while(*endp);
printf("%d\n", sum);
return sum;
}
#包括
#包括
int stringSum(常量字符*s);
内部主(空){
int值,i,大小;
字符*文本[]={“1+2+3+4”,
"7",
"30 + 20 + 10",
"9 + 900 + 90000 + 9000 + 90"
};
尺寸=sizeof(文本)/sizeof(*文本);
对于(i=0;iso),我可以将传入的字符串文本复制到一个可修改的数组中,然后继续工作?例如@user3686360,是的
#include <stdio.h>
#include <stdlib.h>
int stringSum(const char *s);
int main(void){
int value, i, size;
char *text[] = { "1 + 2 + 3 + 4",
"7",
"30 + 20 + 10",
"9 + 900 + 90000 + 9000 + 90"
};
size = sizeof(text)/sizeof(*text);
for(i=0;i<size;i++)
stringSum(text[i]);
return 0;
}
int stringSum(const char *s){
int sum = 0;
char *endp;
do{
int num = strtol(s, &endp, 10);
if(*endp == ' ' || *endp == '+' || *endp == '\0'){
sum += num;
if(*endp)
s = ++endp;
} else { //s has invalid character
fprintf(stderr, "Illegal characters are included.\n");
break;
}
}while(*endp);
printf("%d\n", sum);
return sum;
}