扭曲一根绳子 [英] tokezing a string
问题描述
嗨 -
当我编译并运行这个简单的程序时,我遇到了一个段错误。
(第一次调用strtok时出现seg-fault) 。有什么线索吗?
我的gcc是gcc版本4.1.1 20070105(Red Hat 4.1.1-51)
#include< string .h>
int main()
{
char * token;
char * line ="要分开的行;
char * search =" " ;;
/ *令牌将指向LINE。 * /
token = strtok(line,search);
/ * Token将指向TO。 * /
token = strtok(NULL,search);
}
Amit Gupta写道:
嗨 -
当我编译并运行这个简单的时候我遇到了一个seg-fault程序。
(首次调用strtok时出现seg-fault)。有什么线索吗?
我的gcc是gcc版本4.1.1 20070105(Red Hat 4.1.1-51)
#include< string .h>
int main()
{
char * token;
char * line =" LINE TO BEARARATED";
char * search =" " ;;
/ *令牌将指向LINE。 * /
token = strtok(行,搜索);
/ *令牌将指向TO。 * /
token = strtok(NULL,search);
}
是的,strtok修改了字符串它运行,但字符串文字是
只读。
更改代码使用数组而不是指针
>
char line [] =" LINE to BEARARATED ;;
它会起作用。
1.它不是C ++,它的C.
2.如果你使用的是C ++,那么试着找到std库函数。
3。如果你正在使用C然后 - 不要使用strtok,或谨慎使用
和一些Null和相关内存的额外支票。
- raxit
1月24日下午12:38,Amit Gupta < emaila ... @ gmail.comwrote:
嗨 -
编译时出现seg-fault并运行这个简单的程序。
(第一次调用strtok时出现seg-fault)。有什么线索吗?
我的gcc是gcc版本4.1.1 20070105(Red Hat 4.1.1-51)
#include< string .h>
int main()
{
char * token;
char * line =" LINE TO BEARARATED";
char * search =" ;
/ *令牌将指向LINE。 * /
token = strtok(line,search);
/ * Token将指向TO。 * /
token = strtok(NULL,search);
} - 隐藏引用的文字 - 显示引用的文字 -
是的,先生,
在最后一行下面应为NULL。
--raxit
1月24日下午2:40,raxitsheth2 ... @ yahoo.co.in写道:
1它不是C ++,它的C.
2.如果你使用的是C ++,那么试着找一下std库函数。
3.如果你使用C那么 - 不要使用strtok,或谨慎使用
和一些Null和相关内存的额外支票。
--raxit
1月24日下午12:38,Amit Gupta < emaila ... @ gmail.comwrote:
嗨 -
I编译并运行这个简单的程序时会遇到段错误。
(第一次调用strtok时出现seg-fault)。任何线索?
我的gcc是gcc版本4.1.1 20070105(Red Hat 4.1.1-51)
#include< string.h>
int main()
{
char * token;
char * line =" LINE TO BE SUBARATED" ;;
char * search ="英寸;
/ * Token将指向LINE。 * /
token = strtok(line,search);
/ * Token将指向TO。 * /
token = strtok(NULL,search);
} - 隐藏引用的文字 - 显示引用的文字 - 隐藏引用的文字 - 显示引用的文字 -
Hi -
I get a seg-fault when I compile and run this simple program.
(seg-fault in first call to strtok). Any clues?
My gcc is "gcc version 4.1.1 20070105 (Red Hat 4.1.1-51)"
#include <string.h>
int main()
{
char *token;
char *line = "LINE TO BE SEPARATED";
char *search = " ";
/* Token will point to "LINE". */
token = strtok(line, search);
/* Token will point to "TO". */
token = strtok(NULL, search);
}
Amit Gupta wrote:Hi -
I get a seg-fault when I compile and run this simple program.
(seg-fault in first call to strtok). Any clues?
My gcc is "gcc version 4.1.1 20070105 (Red Hat 4.1.1-51)"
#include <string.h>
int main()
{
char *token;
char *line = "LINE TO BE SEPARATED";
char *search = " ";
/* Token will point to "LINE". */
token = strtok(line, search);
/* Token will point to "TO". */
token = strtok(NULL, search);
}
Yes, strtok modifes the string it operates on, but string literal are
read-only.
Change your code to use an array instead of a pointer
char line[] = "LINE TO BE SEPARATED";
and it will work.
1. Its not C++, Its C.
2.If you are using C++ then try to find std library function.
3.if you are using C then -- Dont use strtok, or use it with caution
and some Extra Checks for Null and related memory stuff.
--raxit
On Jan 24, 12:38 pm, "Amit Gupta" <emaila...@gmail.comwrote:Hi -
I get a seg-fault when I compile and run this simple program.
(seg-fault in first call to strtok). Any clues?
My gcc is "gcc version 4.1.1 20070105 (Red Hat 4.1.1-51)"
#include <string.h>
int main()
{
char *token;
char *line = "LINE TO BE SEPARATED";
char *search = " ";
/* Token will point to "LINE". */
token = strtok(line, search);
/* Token will point to "TO". */
token = strtok(NULL, search);
}- Hide quoted text -- Show quoted text -
Yes Sir,
It should be NULL in below last line.
--raxit
On Jan 24, 2:40 pm, raxitsheth2...@yahoo.co.in wrote:1. Its not C++, Its C.
2.If you are using C++ then try to find std library function.
3.if you are using C then -- Dont use strtok, or use it with caution
and some Extra Checks for Null and related memory stuff.
--raxit
On Jan 24, 12:38 pm, "Amit Gupta" <emaila...@gmail.comwrote:
Hi -
I get a seg-fault when I compile and run this simple program.
(seg-fault in first call to strtok). Any clues?
My gcc is "gcc version 4.1.1 20070105 (Red Hat 4.1.1-51)"
#include <string.h>
int main()
{
char *token;
char *line = "LINE TO BE SEPARATED";
char *search = " ";
/* Token will point to "LINE". */
token = strtok(line, search);
/* Token will point to "TO". */
token = strtok(NULL, search);
}- Hide quoted text -- Show quoted text -- Hide quoted text -- Show quoted text -
这篇关于扭曲一根绳子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!