linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Martin Buchan <M.J.Buchan@gre.ac.uk>
To: linux-c-programming@vger.kernel.org
Subject: Returning pointer to array of structures
Date: Tue, 29 Apr 2003 09:31:22 +0100	[thread overview]
Message-ID: <20030429083122.GA22282@gre.ac.uk> (raw)

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


             reply	other threads:[~2003-04-29  8:31 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-04-29  8:31 Martin Buchan [this message]
2003-04-28 10:57 ` Returning pointer to array of structures 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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20030429083122.GA22282@gre.ac.uk \
    --to=m.j.buchan@gre.ac.uk \
    --cc=linux-c-programming@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).