c 语言编译器 bus error 10,总线错误:10与C中的strtok(Bus Error:10 with strtok in C)

总线错误:10与C中的strtok(Bus Error:10 with strtok in C)

这是我的代码,当我运行代码时,总是出现总线错误10:

void print_tokens(char *line)

{

static char whitespace[] = " \t\f\r\v\n";

char *token;

for(token = strtok(line, whitespace);

token != NULL;

token = strtok(NULL, whitespace))

printf("Next token is %s\n", token);

}

int main(void)

{

char *line = "test test test";

print_tokens(line);

return 0;

}

请帮帮我!

Here is my code, when I running the code, always bus error 10:

void print_tokens(char *line)

{

static char whitespace[] = " \t\f\r\v\n";

char *token;

for(token = strtok(line, whitespace);

token != NULL;

token = strtok(NULL, whitespace))

printf("Next token is %s\n", token);

}

int main(void)

{

char *line = "test test test";

print_tokens(line);

return 0;

}

Please help me!

原文:https://stackoverflow.com/questions/41021149

更新时间:2020-03-01 11:47

最满意答案

strtok将修改它传递的缓冲区; 这是契约性的 :

使用这些功能时要小心。 如果您确实使用它们,请注意:

*这些函数修改他们的第一个参数。

*这些函数不能用于常量字符串。

当你将字符串声明为char *str = "blah blah"; 在C中,你声明它是只读存储器,所以当你将它传递给strtok ,结果是未定义的,因为strtok想要修改缓冲区,但不能,因为它是只读的。

为了解决这个问题,将str声明为一个数组: char str[] = "blah blah";

strtok will modify the buffer it is passed; this is contractual:

Be cautious when using these functions. If you do use them, note that:

* These functions modify their first argument.

* These functions cannot be used on constant strings.

When you declare a string as char *str = "blah blah"; in C, you are declaring it to be read-only memory, so when you pass it to strtok the result is undefined since strtok wants to modify the buffer but can't since it's read-only.

To address this issue declare str as an array instead: char str[] = "blah blah";

2016-12-07

相关问答

strtok相当于(通常定义为): char *strtok(char *str, const char *delim) {

static char *save;

return strtok_r(str, delim, &save);

}

一般来说,你应该直接使用strtok_r而不是strtok ,除非你需要将你的代码移植到只支持strtok POSIX-2001系统之前 strtok is equivalent to (and often defined as): char

...

您正在使用output作为char * in while (output != NULL){

和 output = strtok(NULL, " ");

但output被声明为简单的char char* words, output;

看一看C-FAQ的问题1.5 You are using output as a char * in while (output != NULL){

and output = strtok(NULL, " ");

But output is declared

...

此行不正确: int num = (int)n;

这是int的地址吗? 不,它是字符缓冲区的地址,在你的整数的字符表示被存储的位置,重新解释为一个int (即它可能是一个截断的地址,使它几乎是一个毫无意义的数字)。 您可以通过解析该值或使用atoi将其转换为int: int num = atoi(n);

演示。 This line is incorrect: int num = (int)n;

Is this the address to the int? No, it is an addr

...

你不能修改字符串文字。 关于这个问题的常见问题解释得最好。 简而言之,如果你声明 char *stuff = "Read-only stuff";

你不能修改它。 strtok接受char *这一事实与您不能将数组传递给函数有关 ,您只能传递地址。 另一个常见问题的条目可能有帮助。 You can't modify a string literal. The c faq on the subject explains it best. In a nutshell if you declare c

...

strtok修改原始字符串。 然后你传递一个你无法修改的字符串文字。 您应该声明并初始化cmd如下所示 - char cmd[] = "zwr ^A(\"A\")"; //string: zwr ^A("A")

另外在函数int checkUserRole(char *cmd) - char fileContent[1000000]; // maybe use a pointer instead and allocate memory on heap

strtok modifies th

...

使用strtok()是一个非常糟糕的主意,因为这个函数可能会改变原始字符串,因为它是const ,所以不应该更改它。 此外,即使这可以工作,你也没有预见'\ n'作为后续strtok()循环中的标记分隔符。 因此,最好对齐所有令牌分隔符字符串。 为什么不使用C ++功能: size_t pe;

for (size_t pb=0; pe=MyStr.find_first_of(" ,-\n", pb); pb=pe+1)

{

cout << MyStr.substr(pb, (pe==st

...

strtok将修改它传递的缓冲区; 这是契约性的 : 使用这些功能时要小心。 如果您确实使用它们,请注意: *这些函数修改他们的第一个参数。 *这些函数不能用于常量字符串。 当你将字符串声明为char *str = "blah blah"; 在C中,你声明它是只读存储器,所以当你将它传递给strtok ,结果是未定义的,因为strtok想要修改缓冲区,但不能,因为它是只读的。 为了解决这个问题,将str声明为一个数组: char str[] = "blah blah"; strtok will m

...

strtok为两个参数采用以null结尾的字符串。 你没有为你的分隔符传递一个。 尝试这个: char * getNextToken (char * line){

const char *delim = ",";

return strtok(line, delim);

}

strtok takes a null-terminated string for both arguments. You're not passing one for your delimiter. try

...

问题是你使用fgets() 。 它没有返回你认为它第二次做的事情。 第一次通过, fgets()用"10/23/2014\0"填充line[] ,一切都很好。 但是,第二次通过时, ENTER键仍然在stdin的输入缓冲区中,因为第一个fgets() line[]没有任何空间来读取它,所以第二次fgets()用"\n\0"填充line[] "\n\0"无需等待新的用户输入。 第一次调用strtok(line, "/")因此返回"\n" ( atoi()转换为0),然后下一次调用strtok(NUL

...

您的代码中有大量内容可能在缓冲区末尾运行或取消引用空指针(这可能会导致总线错误)。 对于我来说,重写代码比列出所有错误要短,所以这里是: char *buffer = malloc(1000);

if ( !buffer )

return NULL;

FILE *f = fopen(bookorders, "r");

int c, z;

if ( f ) for (;;)

{

for ( z = 0; z < 999 && (c = fgetc(f)) != EOF && c

...

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值