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=strinici o=str2;
while(straux){
straux=strstr(s trinicio,delimi ter);
if (straux){
*straux='\0';
result=(char **)realloc(resu lt,tokens*sizeo f(char *));
if (!result){
MessageBoxA(NUL L,"Erro em alocar memória para explode","Out of memory",MB_OK|M B_ICONWARNING);
exit(1);
}
result[tokens-1]=(char *)malloc((strle n(strinicio)+1) *sizeof(char));
strcpy(result[tokens-1],strinicio);
strinicio=strau x+strlen(delimi ter);
tokens++;
}
else{
result=(char **)realloc(resu lt,tokens*sizeo f(char *));
if (!result){
MessageBoxA(NUL L,"Erro em alocar memória para explode","Out of memory",MB_OK|M B_ICONWARNING);
exit(1);
}
result[tokens-1]=(char *)malloc(strlen (strinicio)*siz eof(char));
strcpy(result[tokens-1],strinicio);
}
*numberelements =*numberelement s+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...
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=strinici o=str2;
while(straux){
straux=strstr(s trinicio,delimi ter);
if (straux){
*straux='\0';
result=(char **)realloc(resu lt,tokens*sizeo f(char *));
if (!result){
MessageBoxA(NUL L,"Erro em alocar memória para explode","Out of memory",MB_OK|M B_ICONWARNING);
exit(1);
}
result[tokens-1]=(char *)malloc((strle n(strinicio)+1) *sizeof(char));
strcpy(result[tokens-1],strinicio);
strinicio=strau x+strlen(delimi ter);
tokens++;
}
else{
result=(char **)realloc(resu lt,tokens*sizeo f(char *));
if (!result){
MessageBoxA(NUL L,"Erro em alocar memória para explode","Out of memory",MB_OK|M B_ICONWARNING);
exit(1);
}
result[tokens-1]=(char *)malloc(strlen (strinicio)*siz eof(char));
strcpy(result[tokens-1],strinicio);
}
*numberelements =*numberelement s+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...
Comment