I'd like to ask for help with usage of char **. I have a following func, which dynamically allocates pointer a array
and pointers of char[] arrays in it:
Expand|Select|Wrap|Line Numbers
- int sptr(char **ppchr)
- {
- int x = 0, ret = 0;
- char *pchr = NULL, **ptchr = NULL;
- // In place of for() you can imagine enumeration if something, quantity
- // of which is not known during design time. It's in extra func because
- // I'll need recursion (then I'll need extra parameter long * for index
- // forward of course)
- for(x = 0; x < 10;x++)
- {
- // array of pointers allocation
- if((ptchr = (char **)realloc(ppchr, sizeof(char *) * (x + 1))) == NULL)
- {
- printf("ERR realloc\n");
- ret = 1;
- goto CleanExit;
- }
- ppchr = ptchr;
- // char[] allocation
- if((pchr = (char *)malloc(sizeof(char) * (11))) == NULL)
- {
- printf("ERR malloc\n");
- ret = 2;
- goto CleanExit;
- }
- memset(pchr, '\x0', sizeof(pchr));
- ppchr[x] = pchr;
- // fill out chararray
- sprintf(ppchr[x], "Item%d", x);
- }
- CleanExit:
- // freeing is only done if an error occurs, otherwise it manages
- // 'upper' procedure after it processes contents
- if(ret)
- {
- for(x = 0; x < 10;x++)
- if(ppchr[x])
- {
- free(ppchr[x]);
- }
- free(ppchr);
- }
- return ret;
- }
So I moved declaration of **ppchr one level upper:
Expand|Select|Wrap|Line Numbers
- int main(int argc, char* argv[])
- {
- char **ppchr = NULL;
- int x = 0, ret = 0;
- ret = sptr(ppchr);
- printf("sptr ret: %d\n", ret);
- for(x = 0; x < 10;x++)
- {
- printf("Contents %d=%s\n", x, ppchr[x]);
- }
- if(!ret)
- {
- for(x = 0; x < 10;x++)
- if(ppchr[x])
- {
- free(ppchr[x]);
- }
- if(ppchr)
- free(ppchr);
- }
- return 0;
- }
Please explain what's wrong or how to make it work. I though that another way is not to pass initial char** pointer to sptr() and to return it instead (char **sptr(char *some_par()), however I just don't understand why the hell this doesn't work :-(
Please don't advise vectors nor CString etc. I want to make it work in C, because I want to UNDERSTAND it, which usage of vectors doesn't give me.
Thanks a lof for any help.
Mates