c 中的 strtok 有问题
having an issue with strtok in c
我已经编写了一些代码来帮助我在我的程序中删除破折号以获得更大的链表,但是当我调用 class 时,我的代码卡在了分隔符循环中并且没有死掉。我什至不能用 kill 命令杀死它我必须打开一个新的 ssh 客户端
int deliminator(char word[], struct node *root){
struct node *start = (struct node*) malloc(sizeof(struct node));
struct node *trav = (struct node*) malloc(sizeof(struct node));
start->next= trav;
trav->prev = start;
char *token;
token = strtok(word,"-");
while(token){
/* this loop is broken */
printf("%s",token);
struct node *curr = (struct node*) malloc(sizeof(struct node));
strcpy(curr->set, token);
trav->next = curr;
curr->prev = trav;
trav = trav->next;
token = strtok(NULL,"-");
};
root->next = start;
return(0);
};
此外,当我尝试通过循环 运行 不正确地使用 strtok 时
token = strtok(token,"-");它卡在第一个令牌上。我似乎找不到问题,我的一个朋友建议它与链接列表节点有关,但我删除了它们并且我遇到了同样的问题。
我在此代码片段中调用分隔符 class。
int main(int argc, char *argv[]){
struct node *root = (struct node*) malloc(sizeof(struct node));
struct node *trav = (struct node*) malloc(sizeof(struct node));
root->next = trav;
if(argc == 2){
/*only one giant string*/
deliminator(argv[1],root);
while(root->next!= NULL){
printf("%s",root->set);
};
您的代码大部分结构良好,并正确使用 strtok
。但是您不初始化变量或分配节点内的字段。我将您的调用从 strcpy 切换到 strdup,以便您分配内存,并使用 calloc 而不是 malloc,以便将指针初始化为 null。 deliminator中,只需要分配循环内的节点,只保留一个指针trav遍历链表即可,根节点不用管。
我把你留给你弄清楚如何不浪费你并不真正需要的根节点上的内存。您应该只有一个根指针,并将其地址传递给分隔符。此外,您应该在退出之前清理并释放 strdup 中的节点和分配的字符串。
int deliminator(char word[], struct node *root) {
struct node *trav = root;
char *token;
token = strtok(word,"-");
while(token){
/* this loop is fixed! */
printf("DEBUG: %s\n",token);
struct node *curr = calloc(1, sizeof(struct node));
curr->set = strdup(token);
trav->next = curr;
curr->prev = trav;
trav = trav->next;
token = strtok(NULL,"-");
};
return(0);
}
int main(int argc, char *argv[]){
struct node *root = calloc(1, sizeof(struct node));
if(argc == 2){
/*only one giant string*/
deliminator(argv[1],root);
root = root-> next;
while(root != NULL){
printf("%s\n",root->set);
root = root->next;
}
}
}
我已经编写了一些代码来帮助我在我的程序中删除破折号以获得更大的链表,但是当我调用 class 时,我的代码卡在了分隔符循环中并且没有死掉。我什至不能用 kill 命令杀死它我必须打开一个新的 ssh 客户端
int deliminator(char word[], struct node *root){
struct node *start = (struct node*) malloc(sizeof(struct node));
struct node *trav = (struct node*) malloc(sizeof(struct node));
start->next= trav;
trav->prev = start;
char *token;
token = strtok(word,"-");
while(token){
/* this loop is broken */
printf("%s",token);
struct node *curr = (struct node*) malloc(sizeof(struct node));
strcpy(curr->set, token);
trav->next = curr;
curr->prev = trav;
trav = trav->next;
token = strtok(NULL,"-");
};
root->next = start;
return(0);
};
此外,当我尝试通过循环 运行 不正确地使用 strtok 时 token = strtok(token,"-");它卡在第一个令牌上。我似乎找不到问题,我的一个朋友建议它与链接列表节点有关,但我删除了它们并且我遇到了同样的问题。
我在此代码片段中调用分隔符 class。
int main(int argc, char *argv[]){
struct node *root = (struct node*) malloc(sizeof(struct node));
struct node *trav = (struct node*) malloc(sizeof(struct node));
root->next = trav;
if(argc == 2){
/*only one giant string*/
deliminator(argv[1],root);
while(root->next!= NULL){
printf("%s",root->set);
};
您的代码大部分结构良好,并正确使用 strtok
。但是您不初始化变量或分配节点内的字段。我将您的调用从 strcpy 切换到 strdup,以便您分配内存,并使用 calloc 而不是 malloc,以便将指针初始化为 null。 deliminator中,只需要分配循环内的节点,只保留一个指针trav遍历链表即可,根节点不用管。
我把你留给你弄清楚如何不浪费你并不真正需要的根节点上的内存。您应该只有一个根指针,并将其地址传递给分隔符。此外,您应该在退出之前清理并释放 strdup 中的节点和分配的字符串。
int deliminator(char word[], struct node *root) {
struct node *trav = root;
char *token;
token = strtok(word,"-");
while(token){
/* this loop is fixed! */
printf("DEBUG: %s\n",token);
struct node *curr = calloc(1, sizeof(struct node));
curr->set = strdup(token);
trav->next = curr;
curr->prev = trav;
trav = trav->next;
token = strtok(NULL,"-");
};
return(0);
}
int main(int argc, char *argv[]){
struct node *root = calloc(1, sizeof(struct node));
if(argc == 2){
/*only one giant string*/
deliminator(argv[1],root);
root = root-> next;
while(root != NULL){
printf("%s\n",root->set);
root = root->next;
}
}
}