the mission is to separate the string into several fields. each field is limited by ";", cada campo é armazenado num array de strings Only PTR[0] is correct. I think it's because of the address assignment to *ptr[]. How can i do the correct assignment?
int fields(char *line, char *ptrs[], int max_fields ){
int i=0;
int count = 0;
char *aux = line;
while(*line != '\0'){
if(*line == ';')
{
if(i<max_fields)
{
*line='\0';
ptrs[i]=aux;
i++;
aux=line++;
continue;
count++;
}
line++;
}
return count; // retorna o numero de campos totais
}
void print(char *teste2[]){
printf("PTR[0] = %s \n", teste2[0]);
printf("PTR[1] = %s \n", teste2[1]);
printf("PTR[2] = %s \n", teste2[2]);
}
int main() {
int nrCamposIden, campEsper=3;
//char frase[]= "teste; oi ; ze ; primeiro campo; ;terceiro campo \t; ; palavras do quinto campo 1234\n ";
char *teste2[100];
nrCamposIden = fields(frase,teste2,campEsper) ;
printf("Numero campos = %d \n", nrCamposIden);
print(teste2);
return 0;
}
output expected
PTR[0] = teste
PTR[1] = oi
PTR[2] = ze
output i get
PTR[0] = teste
PTR[1] =
PTR[2] =
thanks to everyone :D