
Returning pointer to array of structure from function
Hi,
Could anyone point me in the right direction with this?
I am trying to populate an array of structures with data read in from an
xml file (using libxml2). I have a few methods which parse separate chunks
of the xml file, populate the array, call the next method passing it
whatever is in the array so far, and then returning the array back to the
original caller.I am not getting any errors but the array is never populated.
Here is my struct and some of my code.
struct menuentry
{
char *appsection;
char *appsubmenu;
Quote:
};
typedef struct menuentry menuentry;
menuentry parseSection (xmlDocPtr doc, xmlNodePtr cur, menuentry *mePtr)
{
xmlChar *uri;
cur = cur->xmlChildrenNode;
while (cur != NULL)
{
if ((!xmlStrcmp(cur->name, (const xmlChar *)"submenu")))
{
uri = xmlGetProp(cur, "name");
xmlFree(uri);
mePtr[i].appsubmenu = uri;
printf(" SUBMENU: %s\n", mePtr[i].appsubmenu);
}
cur = cur->next;
}
return *mePtr;
Quote:
}
menuentry parseDoc(char *docname, menuentry *mePtr)
{
xmlDocPtr doc;
xmlNodePtr cur;
doc = xmlParseFile(docname);
/* snipped error checking stuff */
cur = xmlDocGetRootElement(doc);
while (cur != NULL)
{
if ((!xmlStrcmp(cur->name, (const xmlChar *)"root")))
{
*mePtr = parseSection (doc, cur, mePtr);
}
cur = cur->next;
}
xmlFreeDoc(doc);
return *mePtr;
Quote:
}
int main(int argc, char **argv)
{
int n = countElements("menuconfig");
menuentry *mePtr = malloc( n * sizeof (*mePtr) ); /* Gets size of array */
printf ("number of apps: %d\n", n);
*mePtr = parseDoc("menuconfig", mePtr); /* calls method to populate me */
printf("SECTION: %s\n", mePtr[0].appsection); /* Always empty */
return EXIT_SUCCESS;
Quote:
}
The printf statements in the methods print out the stuff i expect it to but
the final printf statement in main() shows the array always being
empty. I think i am getting my understanding of pointers mixed up
here and am returning the wrong stuff or not passing the right stuff
in the first place (like by reference or value or something)
Any help here to show me where im going wrong would be much
appreciated.
Thanks
Martin