简体   繁体   English

将char *拆分为char *数组

[英]Split char* to char * Array

I'm trying to split a char* to an array of char* in C. I'm used to program in Java / PHP OO. 我试图在C中将char *拆分为char *数组。我习惯于在Java / PHP OO中编程。 I know several easy way to do that in these languages but in C... I'm totally lost. 我知道几种使用这些语言的简单方法,但是在C语言中……我完全迷失了。 I often have segfault for hours x) 我经常有数小时的段错误(x)

I'm using TinyXML and getting info from XML File. 我正在使用TinyXML,并从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();

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

The only problem is on 'gates'. 唯一的问题是在“门”上。 The input form XML looks like "gate1/gate2/gate3" or just blank sometimes. 输入形式的XML看起来像“ gate1 / gate2 / gate3”,有时只是空白。

I want gate1 to be in infos->gates[0] ; 我希望gate1在infos-> gates [0]中; etc. I want to be able to list the gates array afterwards.. 等。我希望以后能够列出盖茨阵列。

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 我总是将所有gates [i]初始化为NULL,但是当我进行for(int i = 0; i

Thanks for all. 谢谢大家

It's OK when I've only pointers but when String(char*) / Arrays / Pointers are mixed.. I can't manage =P 当我只有指针但混合使用String(char *)/数组/指针时,这没关系。我无法管理= P

I saw too that we can use something like int *myArray = calloc(NbOfRows, NbOfRows*sizeof(int)); 我也看到我们可以使用类似int * myArray = calloc(NbOfRows,NbOfRows * sizeof(int));的东西。 Why should we declare an array like that.. ? 为什么我们要这样声明一个数组呢? x) X)

Thanks! 谢谢!

The problem that people frequently have with XML is that they assume all the elements are available. 人们使用XML时常遇到的问题是,他们认为所有元素都可用。 That's not always safe. 这并不总是安全的。 Thus this statement: 因此,此语句:

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

Isn't safe to do because you don't actually know if all of those functions actually return valid objects. 这是不安全的,因为您实际上并不知道所有这些函数是否实际上都返回有效对象。 You really need something like the following (which is not optimized for speed, as I don't know the tinyXML structure name being returned at each point and thus am not storing the results once and am rather calling each function multiple times: 您确实需要类似以下内容的代码(它并未针对速度进行优化,因为我不知道在每个点都返回tinyXML结构名称,因此不会一次存储结果,而是多次调用每个函数:

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 should really 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? 您不只是喜欢错误处理吗?

As to your other question: 至于您的其他问题:

I saw too that we can use something like int *myArray = calloc(NbOfRows, NbOfRows*sizeof(int)); 我也看到我们可以使用类似int * myArray = calloc(NbOfRows,NbOfRows * sizeof(int));的东西。 Why should we declare an array like that.. ? 为什么我们要这样声明一个数组呢? x) X)

calloc first initializes the resulting memory to 0, unlike malloc. 与malloc不同, calloc首先将结果内存初始化为0。 If you see above where I set the end of the buffer to '\\0' (which is actually 0), that's because malloc returns a buffer with potentially random (non-zero) data in it. 如果在上面看到我将缓冲区的末尾设置为“ \\ 0”(实际上为0)的原因,那是因为malloc返回了其中包含潜在随机(非零)数据的缓冲区。 calloc will first set the entire buffer to all 0s first, which can be generally safer. calloc首先将整个缓冲区设置为全0,这通常会更安全。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM