Home > Software engineering >  how do i point multiple addresses to an array of strings in c?
how do i point multiple addresses to an array of strings in c?

Time:04-22

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

CodePudding user response:

  1. Fixed the syntax errors by added the missing } and ensured the variable frase with input data was not commented out.
  2. Introduced a constant MAX_FIELDS to hold maximum number of fields and used that both to size the result array teste2 and the max_fields argument when calling fields().
  3. count now returns how many fields were found (which may be >= max_fields); notice before both i and count were counting number of separators ; and not fields.
  4. There is nothing wrong with walking the *line pointer 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 variables i and j.
  5. field() was changed to use a for() instead of while() loop to make emphasize that j is the index we are walking.
  6. Finally, added a count n argument to to print instead of hard-coding the first 3. If you still only want the first 3 arguments change MAX_FIELDS to 3:
#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
  • Related