linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* Returning pointer to array of structures
@ 2003-04-29  8:31 Martin Buchan
  2003-04-28 10:57 ` Rafael Santos
                   ` (2 more replies)
  0 siblings, 3 replies; 10+ messages in thread
From: Martin Buchan @ 2003-04-29  8:31 UTC (permalink / raw)
  To: linux-c-programming

Hi,

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 when i return it to the calling function ( and most
importantly main() ).

Here is my struct and some of my code.
  
struct menuentry  
{
	char *appsection;
	char *appsubmenu;
};

typedef struct menuentry menuentry;

menuentry parseSubmenu (xmlDocPtr doc, xmlNodePtr cur, menuentry *mePtr, int i)
{
	xmlChar *uri;
	cur = cur->xmlChildrenNode;
	printf("I: %d. SECOND SECTION: %s\n", i,  mePtr[i].appsection);
	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;
}

menuentry parseSection (xmlDocPtr doc, xmlNodePtr cur, menuentry *mePtr)
{
	int i=0;
	xmlChar *uri;
	cur = cur->xmlChildrenNode;
	while (cur != NULL) 
	  {
		  if ((!xmlStrcmp(cur->name, (const xmlChar *)"section")))
			 {
				   uri = xmlGetProp(cur, "name");
				   mePtr[i].appsection = uri;
				   xmlFree(uri);
				   printf("SECTION: %s\n", mePtr[i].appsection); /*prints out correct */
				   *mePtr = parseSubmenu(doc, cur, mePtr, i);
				   printf("SECOND SUBMENU: %s\n", mePtr[i].appsubmenu); /* empty */
				   i++;
			 }
		  cur = cur->next;
	  }
	return *mePtr;
}

menuentry parseDoc(char *docname, menuentry *mePtr)
{
	xmlDocPtr doc;
	xmlNodePtr cur;
	doc = xmlParseFile(docname);
	
	/* snipped all error checking code */
	
	cur = xmlDocGetRootElement(doc);
	
	while (cur != NULL) 
	  {
		  if ((!xmlStrcmp(cur->name, (const xmlChar *)"root")))
			 {
				 printf ("happened once\n");
				 *mePtr = parseSection (doc, cur, mePtr);
			 }
		  cur = cur->next;
	  }
	xmlFreeDoc(doc);
	return *mePtr;
}

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;
}


The printf statements in the methods print out the stuff i expect it
to except for the one that prints out "SECOND SUBMENU:" and also
the final printf statement in main() shows the array always being
empty. This tells me i am not returning my pointer properly but i
cant see what im doing wrong. Can anyone see what i am trying to do
and/or where the error lies?

Thanks for any help.

Martin


^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2003-05-01  9:49 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2003-04-29  8:31 Returning pointer to array of structures Martin Buchan
2003-04-28 10:57 ` Rafael Santos
2003-04-30  8:24   ` Martin Buchan
2003-05-01  0:49     ` Glynn Clements
2003-05-01  6:03       ` Rafael Santos
2003-05-01  9:49       ` Martin Buchan
2003-04-29  9:30 ` Glynn Clements
2003-04-29 10:28   ` Martin Buchan
2003-04-29 12:11 ` Jason Cooper
2003-04-29 20:57   ` Glynn Clements

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).