使用C中的strtok字符串解析 [英] String parsing in C using strtok

查看:376
本文介绍了使用C中的strtok字符串解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个小源$ C ​​$ C,用于测试的字符串类似变量解析取得字符串我需要在其他项目中使用。

 的#include<&stdio.h中GT;
#包括LT&;&stdlib.h中GT;
#包括LT&;&string.h中GT;
INT主要(无效)
{
    字符字符串[] =C-AC-2C-3C-BOB;
    字符* S;
    字符*手[3];
    字符* USR;
    S =(字符*)释放calloc(1,sizeof的(炭));
    手[1] =(字符*)释放calloc(3的sizeof(字符));
    手[2] =(字符*)释放calloc(3的sizeof(字符));
    手[3] =(字符*)释放calloc(3的sizeof(字符));
    USR =(字符*)释放calloc(21,sizeof的(炭));    S = strtok的(字符串, - );
    手[1] = strtok的(NULL, - );
    手[2] = strtok的(NULL, - );
    手[3] = strtok的(NULL, - );
    USR =的strtok(NULL,\\ 0);    的printf(%S:%S:%S:%S:%S \\ n,S,手[1],手[2],手[3],USR);    返回0;
}

现在的问题是,我得到了这些 3C:AC:2C:3C:BOB 如printf而不是的C结果:AC:2C: 3C:BOB

-------编辑-----

code没有内存泄漏。问题仍然存在。

 的#include<&stdio.h中GT;
#包括LT&;&stdlib.h中GT;
#包括LT&;&string.h中GT;
INT主要(无效)
{
    字符字符串[] =C-AC-2C-3C-BOB;
    字符* S;
    字符*手[3];
    字符* USR;    S = strtok的(字符串, - );
    手[1] = strtok的(NULL, - );
    手[2] = strtok的(NULL, - );
    手[3] = strtok的(NULL, - );
    USR =的strtok(NULL,\\ 0);    的printf(%S:%S:%S:%S:%S \\ n,S,手[1],手[2],手[3],USR);    返回0;
}


解决方案

您声明数组具有三个条目,那么你索引它使用索引 1 3 。但在C数组具有 0 尺寸-1索引(如 2 你的情况)。

所以,你写/从出数组的边界,导致未定义行为的读/

2 更改您的阵列 0 的指标,它应该工作正常。

I've got this little source code, made for testing the parsing of a string similar to variable string I need to use in other project

#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, "\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:BOB as result of printf instead of 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[1] = strtok (NULL, "-");
    hand[2] = strtok (NULL, "-");
    hand[3] = strtok (NULL, "-");
    usr = strtok (NULL, "\0");

    printf("%s:%s:%s:%s:%s\n", s, hand[1], hand[2], hand[3], usr);

    return 0;
}

解决方案

You declare an array hand as having three entries, then you index it using indexes 1 through 3. But arrays in C have indexes from 0 to size-1 (e.g. 2 in your case).

So you write/read to/from out of bounds of the array, leading to undefined behavior.

Change the indexes of your array to 0 through 2 and it should work fine.

这篇关于使用C中的strtok字符串解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆