简体   繁体   中英

Passing an array of strings from a function to main function (C code)

My C xmlretrive function(thisurl, thisxpath) retrieve an XML from a given URL with cURL and send obtained chunk.memory to libxml2 parser that explorer all nodes with xpath and match thisxpath expression (basically returning some text from some node attributes). It was difficult (I'm a beginner programmer) but with some days of work, some searches in this forum and some help of stack's community I got my code to work. Amazing.. but.. but now I'm fronting my last problem (and sincerely I guessed it would be much easier).

I have my data in xmlretrive, ok, but how to pass that informations back to main function? Populating an array of strings with results then accessing that array from main (and last freeing memory) is a good behavior? How can I accomplish that? Or is there any other "standard/safe" procedure? I've read that is not possible to "return" an array. Some help would be very appreciated, cause actually I'm only able to print results on screen (and inside xmlretrive function of course) :\\

Best, Giovanni.

int main(void)  {
char thisxpath[200];
char thisurl[200];
strcpy (thisurl,"http://api.openweathermap.org/data/2.5/forecast/daily?q=Pescara&mode=xml&units=metric&cnt=3");
strcpy (thisxpath,"//time/@day | //symbol/@name | //windSpeed/@name | //temperature/@*[name()='day' or name()='min']");
xmlretrive (thisurl, thisxpath);
return 0;
}

void xmlretrive(char* myurl, char* myxpath) {
//code, code, a lot of code
for (i=0; i < nodeset->nodeNr; i++) {
    keyword = xmlNodeListGetString(doc, nodeset->nodeTab[i]->xmlChildrenNode, 1);

    printf("keyword: %s\n", keyword);

    // Need to get keyword values back to main//        



xmlFree(keyword);}
//code
}

Copy keywords to a linked-list and return it to main. This way you can iterate and print them in main.

Also, you will not have to know before hand what size the list of keywords will be. Make sure that you copy the keyword string libxml returns into the linked-list node and xmlFree free it to avoid issues with memory management.

Use a pointer. Like this:

    int main(void)  {
char thisxpath[200];
char thisurl[200];
strcpy (thisurl,"http://api.openweathermap.org/data/2.5/forecast/daily?q=Pescara&mode=xml&units=metric&cnt=3");
strcpy (thisxpath,"//time/@day | //symbol/@name | //windSpeed/@name | //temperature/@*    [name()='day' or name()='min']");
char** plop = NULL;
xmlretrive (thisurl, thisxpath, &plop);
return 0;
}

void xmlretrive(char* myurl, char* myxpath, char*** plop) {
//code, code, a lot of code
for (i=0; i < nodeset->nodeNr; i++) {
    keyword = xmlNodeListGetString(doc, nodeset->nodeTab[i]->xmlChildrenNode, 1);

    printf("keyword: %s\n", keyword);  
    /* Malloc plop and initialize it as you want*/


xmlFree(keyword);}
//code
}

It's a bit ugly, but it's much nicer than global variables.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM