本文介绍了斯普利特的char *为char *数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想一个char *拆分为char *的C.数组
我习惯用Java / PHP的面向对象编程。我知道有几家简单的方法,在这些语言但在C ......我完全失去了。我经常有段错误数小时X)

I'm trying to split a char* to an array of char* in C.I'm used to program in Java / PHP OO. I know several easy way to do that in these languages but in C... I'm totally lost. I often have segfault for hours x)

我使用TinyXML的和得到的XML文件信息。

I'm using TinyXML and getting info from XML File.

下面就是我们找到阵列的结构。

Here's the struct where we find the array.

const int MAX_GATES = 64;

typedef struct {
    char *name;
    char *firstname;
    char *date;
    char *id;
    char *gates[MAX_GATES];
} UserInfos;

和这里的地方我填补了这一结构:

And here's where I fill this struct :

    UserInfos * infos = (UserInfos*)malloc(1024);

    infos->firstname = (char*)malloc(256);
    infos->name = (char*)malloc(128);
    infos->id = (char*)malloc(128);
    infos->date = (char*)malloc(128);

    sprintf(infos->firstname, "%s", card->FirstChild("firstname")->FirstChild()->Value());
    sprintf(infos->name, "%s", card->FirstChild("name")->FirstChild()->Value());
    sprintf(infos->date, "%s", card->FirstChild("date")->FirstChild()->Value());
    sprintf(infos->id, "%s", card->FirstChild("filename")->FirstChild()->Value());

    ////////////////////////
    // Gates
    char * gates = (char*) card->FirstChild("gates")->FirstChild()->Value();

    //////////////////////////

唯一的问题是'门'。
输入表单XML看起来像GATE1 / GATE2 / gate3或只是空白的时候。

The only problem is on 'gates'.The input form XML looks like "gate1/gate2/gate3" or just blank sometimes.

我要GATE1是在infos->门[0];等等
我希望能够在事后列出门阵列..

I want gate1 to be in infos->gates[0] ; etc.I want to be able to list the gates array afterwards..

我一直有一个段错误,当我尝试。
顺便说一句,我真的不现在如何初始化数组的指针。我总是初始化所有门[I]为NULL,但似乎我有段错误,当我做
    的for(int i = 0;我

感谢所有。

I always have a segfault when I try.Btw, I don't really now how to initialize this array of pointers. I always initialize all gates[i] to NULL but It seems that I've a segfault when I do for(int i=0;i

Thanks for all.

这是确定的,当我只有三分球,但是当字符串(字符*)/数组/指针混合..我不能管理= P

It's OK when I've only pointers but when String(char*) / Arrays / Pointers are mixed.. I can't manage =P

我还看到,我们可以使用类似
    为int * myArray的=释放calloc(NbOfRows,NbOfRows *的sizeof(INT));
我们为什么要申报这样的一个数组..? X)

I saw too that we can use something like int *myArray = calloc(NbOfRows, NbOfRows*sizeof(int));Why should we declare an array like that.. ? x)

谢谢!

推荐答案

这是人们经常对XML的问题是,他们承担所有的元素都可以。这并不总是安全的。因此,这个语句:

The problem that people frequently have with XML is that they assume all the elements are available. That's not always safe. Thus this statement:

sprintf(infos->firstname, "%s", card->FirstChild("firstname")->FirstChild()->Value());

是不是安全的做法,因为你是不知道如果所有这些
实际上函数返回的有效对象。你真正需要的东西
像下面的(即不可以优化速度,因为我不
知道被人在每一个点,从而返回TinyXml的结构名
我不是存储结果一次和我通话,而每个功能
多次:

Isn't safe to do because you don't actually know if all of thosefunctions actually return valid objects. You really need somethinglike the following (which is not optimized for speed, as I don'tknow the tinyXML structure name being returned at each point and thusam not storing the results once and am rather calling each functionmultiple times:

if (card->FirstChild("firstname") &&
   card->FirstChild("firstname")->FirstChild()) {
   sprintf(infos->firstname, "%s", card->FirstChild("firstname")->FirstChild()->Value());
}

然后,防止缓冲区从你如果数据溢出
真的是这样做的:

And then, to protect against buffer overflows from the data you shouldreally be doing:

if (card->FirstChild("firstname") &&
   card->FirstChild("firstname")->FirstChild()) {
   infos->firstname[sizeof(infos->firstname)-1] = '\0';
   snprintf(infos->firstname, sizeof(infos->firstname)-1, "%s", card->FirstChild("firstname")->FirstChild()->Value());
}

难道你不喜欢的错误处理?

Don't you just love error handling?

至于您的其他问题:

我还看到,我们可以使用类似为int * myArray的=
释放calloc(NbOfRows,NbOfRows *的sizeof(INT));我们为什么要声明一个数组
像那样.. ? X)

释放calloc 首先初始化所产生的内存为0,不像malloc的。
如果你看到上面的,我设置缓冲区'\\ 0'的结束(这是
其实0),那是因为的malloc返回缓冲区可能
在它的随机(非零)的数据。释放calloc将首先设置整个缓冲区
都为0的第一,它可以是一般更安全。

calloc first initializes the resulting memory to 0, unlike malloc.If you see above where I set the end of the buffer to '\0' (which isactually 0), that's because malloc returns a buffer with potentiallyrandom (non-zero) data in it. calloc will first set the entire bufferto all 0s first, which can be generally safer.

这篇关于斯普利特的char *为char *数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 07:54