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 .