Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7278427
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T22:52:23+00:00 2026-05-28T22:52:23+00:00

I’m trying to split a char* to an array of char* in C. I’m

  • 0

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)

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();

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

The only problem is on ‘gates’.
The input form XML looks like “gate1/gate2/gate3” or just blank sometimes.

I want gate1 to be in 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

Thanks for all.

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

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)

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T22:52:24+00:00Added an answer on May 28, 2026 at 10:52 pm

    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());
    

    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:

    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)); Why should we declare an array
    like that.. ? x)

    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 is
    actually 0), that’s because malloc returns a buffer with potentially
    random (non-zero) data in it. calloc will first set the entire buffer
    to all 0s first, which can be generally safer.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create an if statement in PHP that prevents a single post
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
I used javascript for loading a picture on my website depending on which small

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.