C语言 使用strtok在C中解析字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18183633/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
String parsing in C using strtok
提问by Arrigo Pierotti
I've got this little source code, made for testing the parsing of a string similar to variable stringI need to use in other project
我有这个小源代码,用于测试类似于string我需要在其他项目中使用的变量的字符串的解析
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main (void)
{
char string[] = "C-AC-2C-3C-BOB";
char* s;
char* hand[3];
char* usr;
s = (char*) calloc(1, sizeof(char));
hand[1] = (char*) calloc(3, sizeof(char));
hand[2] = (char*) calloc(3, sizeof(char));
hand[3] = (char*) calloc(3, sizeof(char));
usr = (char*) calloc(21, sizeof(char));
s = strtok (string,"-");
hand[1] = strtok (NULL, "-");
hand[2] = strtok (NULL, "-");
hand[3] = strtok (NULL, "-");
usr = strtok (NULL, "#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main (void)
{
char string[] = "C-AC-2C-3C-BOB";
char* s;
char* hand[3];
char* usr;
s = strtok (string,"-");
hand[1] = strtok (NULL, "-");
hand[2] = strtok (NULL, "-");
hand[3] = strtok (NULL, "-");
usr = strtok (NULL, "hand[3] = strtok (NULL, "-");
^
printf("%s:%s:%s:%s:%s\n", s, hand[1], hand[2], hand[3], usr);
^
wrong index value
");
printf("%s:%s:%s:%s:%s\n", s, hand[1], hand[2], hand[3], usr);
return 0;
}
");
printf("%s:%s:%s:%s:%s\n", s, hand[1], hand[2], hand[3], usr);
return 0;
}
The problem is that i got these 3C:AC:2C:3C:BOBas result of printf instead of C:AC:2C:3C:BOB.
问题是我得到这些3C:AC:2C:3C:BOB结果是 printf 而不是C:AC:2C:3C:BOB.
-------EDIT-----
- - - -编辑 - - -
Code without memory leaks. Problem remains
没有内存泄漏的代码。问题依旧
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main (void)
{
char string[] = "C-AC-2C-3C-BOB";
char* s;
char* hand[3];
char* usr;
s = strtok (string,"-");
hand[0] = strtok (NULL, "-");
hand[1] = strtok (NULL, "-");
hand[2] = strtok (NULL, "-");
usr = strtok (NULL, "+-----------------------+
| ...
|
|0x***00 hand[0]
|
|
|
|0x***04 hand[1]
|
|
|
|0x***08 hand[2]
|
|
|
|0x***0C hand[3]
| <---hander[1] pointer this address
|
|______ hand[3] end here
");
printf("%s:%s:%s:%s:%s\n", s, hand[0], hand[1], hand[2], usr);
return 0;
}
采纳答案by Some programmer dude
You declare an array handas having three entries, then you index it using indexes 1through 3. But arrays in C have indexes from 0to size-1(e.g. 2in your case).
您将一个数组声明hand为具有三个条目,然后使用1通过 的索引对其进行索引3。但是 C 中的数组的索引从0到size-1(例如2在您的情况下)。
So you write/read to/from out of bounds of the array, leading to undefined behavior.
因此,您在数组范围外写入/读取/读取,导致未定义的行为。
Change the indexes of your array to 0through 2and it should work fine.
将数组的索引更改为0through 2,它应该可以正常工作。
回答by Grijesh Chauhan
In your code you have out-of index problem that causes Undefined behavior at runtime:
在您的代码中,您存在索引不足问题,导致运行时出现未定义行为:
s = (char*) calloc(1, sizeof(char));
hand[1] = (char*) calloc(3, sizeof(char));
hand[2] = (char*) calloc(3, sizeof(char));
hand[3] = (char*) calloc(3, sizeof(char));
usr = (char*) calloc(21, sizeof(char));
Remember index value in array starts with 0according to declaration char* hand[3];index values can be 0 to 2
记住数组中的索引值0根据声明char* hand[3];索引值可以是 0 到 2
回答by nouney
You don't need to callocyour pointers, since strtok()will return a valid memory address (actually strtok()modifies the string and replace the separator by an null char).
The other problem is the indexes of the array: in C, an index starts at 0. The first element of handis hand[0]and the last one is hand[2].
你不需要calloc你的指针,因为strtok()它将返回一个有效的内存地址(实际上strtok()修改字符串并用空字符替换分隔符)。另一个问题是数组的索引:在 C 中,索引从 0 开始。第一个元素hand是hand[0],最后一个元素是hand[2]。
回答by Lidong Guo
Here is the stack of you program:
这是您程序的堆栈:
##代码##So the hand[3] use the address the cover the *hand[1] , this is the 3Ccome
所以 hand[3] 使用地址覆盖 *hand[1] ,这是3C来的
回答by isayme
Firstly, you should annotation these lines to avoid memory leaks :
首先,您应该注释这些行以避免内存泄漏:
##代码##Then, after change the code, I build and run the result in Windows and Linux, neither got your unexpected print result.
然后,更改代码后,我在Windows和Linux上构建并运行结果,都没有得到您意想不到的打印结果。

