Problem with storing and printing strings in C

1

I'm picking up words from a text file, and at the time of printing they're all right except when I store them in a vector. If I store them in a vector, at the time of printing, some random characters come out before the words.

void leArquivo(FILE* f, int numPalavras){
char palavra[30];
int flag = 0, i = 0, terminou = 0, cont = 0;
char** vetorDesord = malloc(sizeof(char)*(numPalavras-1));
    while(!terminou){
    flag = fscanf(f, "%s", palavra);
    if (flag != EOF){
         removeChar(palavra, '.', ',', ' ', ';', '"');
         palavra[0] = tolower(palavra[0]);
         vetorDesord[i] = malloc(sizeof(char)*strlen(palavra));
         vetorDesord[i] = palavra;
        printf("%s", palavra);
        i++;
    }
    else terminou = 1;
    cont++;
}

I've already tested:

  • Without storing in vector, print everything correct.
  • Without the functions tolower() and removeChar() , the error remains the same
  • Storing in vector, anything I print in this excerpt (ex: printf("a") ) also prints with error
  • I used strcpy() , error persisted
  • asked by anonymous 17.11.2018 / 18:13

    1 answer

    1

    It has several errors and the solution is more complex than necessary and very inefficient, in addition to avoiding the standard that is usually done in C.

    Usually we allocate the necessary memory where it needs and we move to the manipulate function. We pretty much avoid using malloc() , mainly because people forget to give free() , which seems to be the case. Even if you do not want to pass the already allocated vector, it looks like you could allocate it in your function.

    I would not do this because the function is reading and printing data, they are distinct things, this is generally considered wrong.

    I did not write the function that removes characters, I hope it is simple and correct, but I'm sure not. And it is there that should make the letters small, besides being more efficient is correct, what you are doing in your code is to use a function that converts a character to try to do in the whole word, it does not work, at most it will do in the first character of the word.

    Your code gives you trouble if you have more words than what you expect. The loop control is too complex. And there are too many variables. There are other improvements to make.

    And if you're going to use malloc() yourself, it does not make sense to use sizeof(char) because it's always 1 . And using strlen() is almost always error . And here you would have to use strcpy() , which is more of an inefficiency.

    #include <stdio.h>
    
    int leDados(FILE* arquivo, int numPalavras, char palavras[numPalavras][31]) {
        for (int i = 0; i < numPalavras; i++) {
            if (fscanf(arquivo, "%30s", palavras[i]) == EOF) return i;
            //removeCharETornaMinuscula(palavra, '.', ',', ' ', ';', '"');
        }
        return 0;
    }
    
    int main(void) {
        char palavras[10][31];
        int numPalavras = leDados(stdin, 10, palavras);
        for (int i = 0; i < numPalavras; i++) printf("%s\n", palavras[i]);
    }
    

    See running on ideone . And no Coding Ground . Also I put it in GitHub for future reference .

        
    18.11.2018 / 12:44