I have a list of strings, and a function which is supposed to add one in the tail of it.
List is initialised this way:
Function called like this (nIstate is an integer I'll use later in order to manipulate other lists):
And finally, this is the function:
I can't understand why doesn't it work, it fails tryng to make *lst[i+i] point to NULL.
Testing with gdb i've seen that strcopy works fine.
First time I call the function, for loop finishes with i=0, so realloc gives space for two (char *).
strlen(str) bytes are reserved for the first one, and the second one is supposed to point NULL, but then is when the program fails...
Can anybody help me?. Thank you
List is initialised this way:
Code:
char **Istate=NULL; Istate=malloc(sizeof (char *)); Istate[0]=NULL;
Code:
nIstate=appendList(&Istate,buff);
Code:
int appendList(char ***lst,char *str){
int i;
char **auxlst=NULL;
for(i=0;*lst[i]!=NULL;i++);
auxlst=realloc(*lst, (i+2)*sizeof (char *));
if(auxlst==NULL){
fprintf(stderr,"La cagaste\n");
free(lst);
return -1;
}
*lst=auxlst;
*lst[i]=malloc(strlen(str));
strcpy(*lst[i],str);
*lst[i+1]=NULL;
return i;
}
Testing with gdb i've seen that strcopy works fine.
First time I call the function, for loop finishes with i=0, so realloc gives space for two (char *).
strlen(str) bytes are reserved for the first one, and the second one is supposed to point NULL, but then is when the program fails...
Can anybody help me?. Thank you
Comment