我试图理解为什么下面的代码片段给出了分段错误:
void tokenize(char* line)
{
char* cmd = strtok(line," ");
while (cmd != NULL)
{
printf ("%s\n",cmd);
cmd = strtok(NULL, " ");
}
}
int main(void)
{
tokenize("this is a test");
}
Run Code Online (Sandbox Code Playgroud)
我知道strtok()实际上并没有对字符串文字进行标记,但在这种情况下,line直接指向"this is a test"内部为数组的字符串char.是否有任何令牌化line而不将其复制到数组中?
我对在C中的char指针上调用strtok时会发生什么感到有些困惑.我知道它会修改字符串的内容,所以如果我在名为'line'的变量上调用strtok,它的内容将会改变.假设我遵循以下方法:
void function myFunc(char* line) {
// get a pointer to the original memory block
char* garbageLine = line;
// Do some work
// Call strtok on 'line' multiple times until it returns NULL
// Do more work
free(garbageLine);
}
Run Code Online (Sandbox Code Playgroud)
进一步假设'line'在传递给myFunc之前被malloced.我应该在使用strtok之后释放原始字符串还是为我们完成工作?另外,如果'line'没有被malloced并且我尝试使用上面的函数会发生什么?取而代之的是更安全吗?(假设程序员知道该行未被malloced,则不会免费拨打电话)
调用
char* garbageLine = line;
myFunc(line);
free(garbageLine);
Run Code Online (Sandbox Code Playgroud)
功能定义
void function myFunc(char* line) {
// Do some work
// Call strtok on 'line' multiple times until it returns NULL
// Do more work
}
Run Code Online (Sandbox Code Playgroud) strtok_r是strtok的可重入变体.符合POSIX标准.但是,MinGW缺少它,我正在尝试编译正在使用它的程序.
有没有什么办法可以添加这个函数的标准实现,可能是项目自己的代码,还是MinGW的标准库函数?
我的字符串是"A,B,C,D,E"
并且分隔符是","
如何在执行strtok()之后获取剩余的字符串,即"B,C,D,E"
char a[] = "A,B,C,D,E";
char * separator = ",";
char * b = strtok(a,separator);
printf("a: %s\n", a);
printf("b: %s\n", b);
Run Code Online (Sandbox Code Playgroud)
输出为:
a:A
b:A
但是如何得到结果
a:B,C,D,E
b:
谢谢.
或者更确切地说,strtok如何生成它的返回值指向的字符串?它是否动态分配内存?我问,因为我不确定是否需要在以下代码中释放令牌:
STANDARD_INPUT变量用于退出过程,以防我内存耗尽以进行分配,字符串是测试对象.
int ValidTotal(STANDARD_INPUT, char *str)
{
char *cutout = NULL, *temp, delim = '#';
int i = 0; //Checks the number of ladders in a string, 3 is the required number
temp = (char*)calloc(strlen(str),sizeof(char));
if(NULL == temp)
Pexit(STANDARD_C); //Exit function, frees the memory given in STANDARD_INPUT(STANDARD_C is defined as the names given in STANDARD_INPUT)
strcpy(temp,str);//Do not want to touch the actual string, so copying it
cutout = strtok(temp,&delim);//Here is the lynchpin -
while(NULL != cutout)
{
if(cutout[strlen(cutout) - 1] …Run Code Online (Sandbox Code Playgroud) 可能重复:
strtok不接受:char*str
使用该strtok功能时,使用char *而不是char []导致分段错误.
这运行正常:
char string[] = "hello world";
char *result = strtok(string, " ");
Run Code Online (Sandbox Code Playgroud)
这会导致分段错误:
char *string = "hello world";
char *result = strtok(string, " ");
Run Code Online (Sandbox Code Playgroud)
任何人都能解释导致这种行为差异的原因吗?
假设我正在使用strtok()这样的..
char *token = strtok(input, ";-/");
Run Code Online (Sandbox Code Playgroud)
有没有办法找出实际使用哪个令牌?例如,如果输入类似于:
Hello there; How are you? / I'm good - End
我可以找出每个令牌使用哪个分隔符?我需要能够输出特定的消息,具体取决于令牌后面的分隔符.
我正在使用strtok将字符串拆分为标记.有谁知道实际计算令牌数量的任何功能?
我有一个命令字符串,我需要拆分它并传递参数execve().
谢谢!
编辑
execve将参数作为char**,所以我需要分配一个指针数组.我不知道有多少分配而不知道有多少令牌.
以下代码在64位和32位上的工作方式不同,这使我无法移植代码.
char * tmp = "How are you?";
printf("size of char * = %ld and size of strtok return val = %ld \n",sizeof(char *),sizeof(strtok(tmp," ")));
Run Code Online (Sandbox Code Playgroud)
以下是输出:
32 bit:
size of char * = 4 and size of strtok return val = 4
64 bit:
size of char * = 8 and size of strtok return val = 4
Run Code Online (Sandbox Code Playgroud)
strtok的手册页说:
#include <string.h>
char *strtok(char *str, const char *delim);
RETURN VALUE
The strtok() and strtok_r() functions return a pointer to the next …Run Code Online (Sandbox Code Playgroud)