'how do i point multiple addresses to an array of strings in c?
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
Solution 1:[1]
- Fixed the syntax errors by added the missing
}and ensured the variablefrasewith input data was not commented out. - Introduced a constant
MAX_FIELDSto hold maximum number of fields and used that both to size the result arrayteste2and themax_fieldsargument when callingfields(). countnow returns how many fields were found (which may be>= max_fields); notice before bothiandcountwere counting number of separators;and not fields.- There is nothing wrong with walking the
*linepointer as you did, but as you need both a start and stop of the current found string, it's seems more clear to use two index variablesiandj. field()was changed to use afor()instead ofwhile()loop to make emphasize thatjis the index we are walking.- Finally, added a count
nargument to to print instead of hard-coding the first 3. If you still only want the first 3 arguments changeMAX_FIELDSto3:
#include <stdio.h>
#define MAX_FIELDS 100
unsigned fields(char *line, char *ptrs[], unsigned max_fields ) {
unsigned count = 0;
unsigned i = 0;
for(unsigned j = 0; line[j]; j++) {
if(line[j] == ';') {
if(count < max_fields) {
line[j] = '\0';
ptrs[count] = line + i;
i = j + 1;
}
count++;
}
}
if(*line) ptrs[count++] = line + i;
return count; // retorna o numero de campos totais
}
void print(char *teste2[], unsigned n) {
for(unsigned i = 0; i < n; i++)
printf("PTR[%u] = %s \n", i, teste2[i]);
}
int main() {
int nrCamposIden, campEsper=MAX_FIELDS;
char frase[]= "teste; oi ; ze ; primeiro campo; ;terceiro campo \t; ; palavras do quinto campo 1234\n ";
char *teste2[MAX_FIELDS];
nrCamposIden = fields(frase, teste2, campEsper) ;
printf("Numero campos = %d \n", nrCamposIden);
print(teste2, nrCamposIden < MAX_FIELDS ? nrCamposIden : MAX_FIELDS);
return 0;
}
and here is the output:
Numero campos = 8
PTR[0] = teste
PTR[1] = oi
PTR[2] = ze
PTR[3] = primeiro campo
PTR[4] =
PTR[5] = terceiro campo
PTR[6] =
PTR[7] = palavras do quinto campo 1234
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
