Hi guys. I have this code:
char **explode(char *delimiter, char *str, int *numberelements){
char *str2;
char *straux=NULL;
char *strinicio=NULL;
char **result=NULL;
int tokens=1;
str2=(char *)malloc(strlen(str)+1);
strcpy(str2,str);
*numberelements=0;
straux=strinicio=str2;
while(straux){
straux=strstr(strinicio,delimiter);
if (straux){
*straux='\0';
result=(char **)realloc(result,tokens*sizeof(char *));
if (!result){
MessageBoxA(NULL,"Erro em alocar memória para explode","Out of memory",MB_OK|MB_ICONWARNING);
exit(1);
}
result[tokens-1]=(char *)malloc((strlen(strinicio)+1)*sizeof(char));
strcpy(result[tokens-1],strinicio);
strinicio=straux+strlen(delimiter);
tokens++;
}
else{
result=(char **)realloc(result,tokens*sizeof(char *));
if (!result){
MessageBoxA(NULL,"Erro em alocar memória para explode","Out of memory",MB_OK|MB_ICONWARNING);
exit(1);
}
result[tokens-1]=(char *)malloc(strlen(strinicio)*sizeof(char));
strcpy(result[tokens-1],strinicio);
}
*numberelements=*numberelements+1;
}
return(result);
}
I wanna know if there is something wrong with this. Cause my program is closing sometimes and must to be something with this fuction... But i really don't find any error...