-   Programming (
-   -   returning data to main() via a pointer to a pointer. (

slzckboy 05-29-2005 08:07 AM

returning data to main() via a pointer to a pointer.

When my function storefiles() returns to main ,the names of the files that it should have stored in my arguement vector are not there,or at most only one is there.

As you can see in storefile() there is a for loop that steps along the arguement vector to make sure all the files that should be there are there.
This works fine.

But if i do the same thing in main() once storefiles has returned, none;or maybe one or two of the filenames are there ??.

I was thinking about making "store" external but I thought pointers negated the need for this?

I added retrun store line ,equating it to the store variable in main() thinking that realloc was moving the storage to a different place,but this didn't help

thanks in advance.


char **storefiles(struct dirent *dS,char **store,int count,int len)

        int c;
        if((store=(char**)realloc(store,sizeof(char *) * c))==NULL)
        return -1;
        if((*(store+count)=(char *)malloc(sizeof(char *) * len))==NULL)
        return -1;
        for(c=0;*(store+c) !=0;c++)
        printf("%s found.\n",*(store+c));
        return store;


eddiebaby1023 05-29-2005 07:17 PM

I think you've confused your use of store. I'd have declared it as char *store[] and used it as *store[n] to load the array (think of it as argv). It's late and my brain's not fully functional, but hopefully this'll give you a clue to get you moving again.;)


slzckboy 05-30-2005 03:40 AM


I thought the syntax **buffer and *buffer[] were the same...!???

oh well..

I will give it a bash.


slzckboy 05-30-2005 01:20 PM

I split up the re-sizing of the buffer **store from the copying of the file name from dS->d_name to *store.

I think its cleaner.

I think the problem was because I was'nt explicitly equating the memory blocks in main() to the reallocated memmory blocks in my function??!?!!

I have done this with an explicit return and cast.

It has definitely given me food for thought anyway...
I also used the syntax suggested by eddiebaby1023 in the calling function in main() which does clean things up I think.
Although I didn't change the declaration. ;0).

Anyway it works now.





int n;
char **filelist;



filelist[n]=save(filelist[n],dStruct->d_name)  ....



char **newbuFFER(char **store,int count)
        int n;
        if ((store=(char **)realloc(store,sizeof(char **) * n))==NULL)
        return NULL;
        return (char **)store;

char *save(char *store,char *source)

        int len;
        if((store=(char *)malloc(sizeof(char *) * len))==NULL)
        return NULL;
        return (char *)store;

All times are GMT -5. The time now is 11:39 AM.