Returning pointer to array of structure from function 
Author Message
 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



Fri, 14 Oct 2005 21:03:52 GMT  
 Returning pointer to array of structure from function
Hi again,

In the interest of keeping my original post down in length, i had snipped
some important stuff out of the last post. Here is the proper code for
the parseSection() function.

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);
                                   *mePtr = parseSubmenu(doc, cur, mePtr, i);
                                   i++;
                         }
                  cur = cur->next;
          }
        return *mePtr;

Quote:
}

which shows that there is a parseSubmenu() function as well which is similar
to parseSection, also returning *mePtr

Sorry for the c*ck up. Any help still greatly appreciated.

Thanks
Martin



Fri, 14 Oct 2005 21:32:43 GMT  
 Returning pointer to array of structure from function

Quote:

> Hi again,

> In the interest of keeping my original post down in length, i had snipped
> some important stuff out of the last post. Here is the proper code for
> the parseSection() function.

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

You are saving a pointer, and then, apparently, freeing the memory it
points to.  This can't be good.


Sat, 15 Oct 2005 12:18:27 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Pointer to function returning pointer to function returning...

2. Having difficulty creating a function that returns an array of structures

3. Pointers: return of pointer to array of pointers to main

4. C - Arrays, Structures, Pointers and Functions !!

5. Question about signal()/pointers to functions that return pointers to functions

6. Arrays, Functions, and Returning Pointers via Parameters

7. function returning pointer array

8. Pointers, Arrays, and function returns

9. Returning and manipulating array/pointer from function to main

10. Return Structure or return Pointer??

11. Returning Int array pointers from functions

12. Function returns pointer to dynamic array

 

 
Powered by phpBB® Forum Software