0
I am trying to return an array of strings to use later in main.
Here is my function:
char **separar_palavras2() {
setlocale(LC_ALL, "Portuguese");
FILE *arquivo;
arquivo = fopen(ftexto, "r");
int i = 0, j = 0;
char* linhas[50];
char line[200];
char *palavra_linha[200];
char *palavras_finais[200];
char *palavras_finais2[200] = {NULL};
char points[200];
int rep = 0;
int p = 0;
while(fgets(line, sizeof line, arquivo) != NULL) {
int w = 0;
while (line[w])
{
if (ispunct (line[w])) {
points[p] = line[w];
p++;
}
w++;
}
linhas[i] = strdup(line); // Separa as linhas do texto e guarda na string "linhas"
palavra_linha[i] = strtok (linhas[i], " \n\r.,!?");
while (palavra_linha[i] != NULL) {
palavras_finais[j] = palavra_linha[i];
j++;
palavra_linha[i] = strtok (NULL, " \n\r.,!?");
}
i++;
}
j = 0;
for(int k = 0; palavras_finais[k] != NULL; k++) {
if (k == 0) {
palavras_finais2[0] = palavras_finais[k];
} else {
rep = 0;
for (int a = 0; palavras_finais2[a] != NULL; a++) {
if (strcmp(palavras_finais[k], palavras_finais2[a]) == 0) {
rep++;
}
}
if (rep == 0) {
j++;
palavras_finais2[j] = palavras_finais[k];
}
}
}
printf("-- Sinais de Pontuação no texto --\n");
for (int b = 0; b < p; b++) {
printf("%c ", points[b]);
}
printf("\n\n-- Palavras no texto --\n\n");
for(int m=0; palavras_finais2[m] != NULL; m++) {
printf("%s\n", palavras_finais2[m]);
}
return palavras_finais2;
}
This is the main:
char**palavras_finais;
int size = 0;
palavras_finais = create_dyn_array_strings(palavras_finais, &size);
char** palavras_finais = separar_palavras2();
Here is the function to allocate memory for the 'palavras_final' variable':
char ** create_dyn_array_strings(char **pps, int *psize) {
char **ppnew= NULL;
int i;
if(pps == NULL){
*psize=30;
ppnew = (char**) calloc(*psize, sizeof(char*));
return ppnew;
}
*psize *=2;
ppnew=(char**) realloc(pps , *(psize)*sizeof(char*));
for(i=*psize-1; i >= (*psize-30); i--)
{
*(ppnew+i)=NULL;
}
return ppnew;
}
Sorry, I am new in c. If it is not possible to return the string array, I would like to be shown an alternative to use later in the main function result.
Without addressing your main problem, I leave a hint: there is no reason to use "200" in size in your temporary data structures: it is very easy a file. txt have end-of-line markings only in paragraphs, and have paragraphs much larger than that. You can put a higher number there, or the program will fail with almost any trivial text file. Some 60,000 would be a good number, and could support paragraphs that take up several pages (such as those occurring in court proceedings). Already, 5 structures of 60000 bytes instead of.
– jsbueno
What language have you programmed? I took a look at this code - I didn’t think it was an expensive mistake, but it makes a salad with the use of memory that would make it unviable for use in Prod. , although for a small script can give the desired results.
– jsbueno
A hint here is to put a mask on the intermediate printf, - make sure things are happening as you expect within the loop that checks repeated words (what you use the variable
a)– jsbueno
Your code also doesn’t show how you try use the structure returned by the Toke function. The error may be there. (The line
palavras_finais = create_dyn_array_strings(palavras_finais, &size);as well as the joint function has no effect on the final program, since the variablepalavras_finaisis over-written on the next line)– jsbueno