I need to create a separate function that will read the string, break it up into sentences, save them into a two-dimensional array and delete the same sentences. How to make it so that I could pass a two-dimensional array created in the reedAndSort function to the main function?

#include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> int reedAndSort (int *numberOfStr, char ***text); int main(){ int i = 0; char **text; int numberOfStr = 0; reedAndSort (&numberOfStr, &text) ; } int reedAndSort (int *numberOfStr, char ***text){ *text = malloc(sizeof(char**)); char *string = malloc(sizeof(char*)); char symbol; int schet = 0; // счСтчик для поиска ΠΎΠ΄ΠΈΠ½Π°ΠΊΠΎΠ²Ρ‹Ρ… строк int j = 0; int i = 0; int lenStr = 1; while (symbol != '\n'){ while ((symbol != '.') && (symbol != '\n')){ symbol = getchar(); lenStr++; string = (char *)realloc(string, lenStr*sizeof(string)); string[lenStr - 2] = symbol; } symbol = getchar(); string[lenStr - 1] = '\0'; for (i = 0; i < *numberOfStr; i++){ if (strlen(string) == strlen((*text)[i])){ for(j = 0; j < strlen(string); j++){ if (tolower(string[j]) == tolower((*text)[i][j])) schet++; } if (schet == strlen(string)) break; else schet = 0; } } if (schet != strlen(string)){ *numberOfStr++; *text = (char**)realloc(*text, *numberOfStr*sizeof(char*)); (*text)[*numberOfStr - 1] = (char*)realloc(*text[*numberOfStr - 1], strlen(string)*sizeof(char)+1); strcpy((*text)[*numberOfStr - 1], string); } schet = 0; free(string); string = NULL; lenStr = 1; //for(i = 0; i < *numberOfStr; i++) printf("%s\n", *text[i]); } } 

PS I'm just learning to write in C, for this I will be glad to any criticism.

  • Criticism: 1) it is better to first read the symbol, and then check it; 2) after entering the entire line, you read the extra character (so, on the second call, you lose the first character); 3) to search for duplicates case-insensitive, it is better to call strcasecmp() ; 4) *numberOfStr++; increases the pointer, it is necessary (*numberOfStr)++; 5) (*text)[*numberOfStr - 1] = (char*)realloc(... does not make sense, you just need to save a pointer to the sentence there and then not free this memory ( strcpy(...) after that needed); 6) there is no return in the function ??? - avp

1 answer 1

You return the accumulated text (an array of pointers to sentences) to main correctly, but I do not see two-dimensional arrays here.

There are errors in your code and it is (to put it mildly) not unambiguous ... The main (fatal) error is *numberOfStr++; . Here you increment the pointer, not the number to which it points (it will be correct - (*numberOfStr)++; ).

It seems to me that the whole function may look something like this:

 int reedAndSort (int *numberOfStr, char ***text) { *text = 0; char *str = 0; // Ρ‚Π΅ΠΊΡƒΡ‰Π΅Π΅ ΠΏΡ€Π΅Π΄Π»ΠΎΠΆΠ΅Π½ΠΈΠ΅ int ls = 0, // Π΅Π³ΠΎ Π΄Π»ΠΈΠ½Π° nt = 0, // количСство ΠΏΡ€Π΅Π΄Π»ΠΎΠΆΠ΅Π½ΠΈΠΉ Π² text[] freestr, // ΠΏΡ€ΠΈΠ·Π½Π°ΠΊ, Π²ΠΊΠ»ΡŽΡ‡Π΅Π½ΠΎ Π»ΠΈ послСднСС ΠΏΡ€Π΅Π΄Π»ΠΎΠΆΠ΅Π½ΠΈΠ΅ Π² text[] c; // ΠΏΡ€ΠΎΡ‡ΠΈΡ‚Π°Π½Π½Ρ‹ΠΉ ΠΈΠ· stdin символ do { c = getchar(); // ΠΊΠΎΠ½Π΅Ρ‡Π½ΠΎ, realloc для ΠΊΠ°ΠΆΠ΄ΠΎΠ³ΠΎ символа ΠΆΡƒΡ‚ΠΊΠΎ Π½Π΅ эффСктивно, // ΠΎΡ‚Π»ΠΎΠΆΠΈΠΌ ΠΎΠΏΡ‚ΠΈΠΌΠΈΠ·Π°Ρ†ΠΈΡŽ Π½Π° ΠΏΠΎΡ‚ΠΎΠΌ... -) (str = realloc(str, ++ls + 1))[ls - 1] = (c == EOF) ? 0 : c; if (c == EOF || c == '\n' || c == '.') { // ΠΊΠΎΠ½Π΅Ρ† прСдлоТСния // Ссли пустоС ΠΏΡ€Π΅Π΄Π»ΠΎΠΆΠ΅Π½ΠΈΠ΅ ΠΈΠ»ΠΈ Π΄ΡƒΠ±Π»ΠΈΠΊΠ°Ρ‚ окаТСтся послСдним, Ρ‚ΠΎ Π½Π°Π΄ΠΎ Π±ΡƒΠ΄Π΅Ρ‚ ΠΎΡΠ²ΠΎΠ±ΠΎΠ΄ΠΈΡ‚ΡŒ ΠΏΠ°ΠΌΡΡ‚ΡŒ freestr = 1; if (ls > 1) { str[ls] = 0; // Π° Ρ‚Π΅ΠΏΠ΅Ρ€ΡŒ сдСлаСм Π½Π°ΠΊΠΎΠΏΠ»Π΅Π½Π½Ρ‹Π΅ символы сишной строкой int i = 0; while (i < nt) // ΠΈΡ‰Π΅ΠΌ Π΄ΡƒΠ±Π»ΠΈΠΊΠ°Ρ‚ if (strcasecmp(str, (*text)[i++]) == 0) break; if (i == nt) { // это Π½Π΅ пустоС ΠΏΡ€Π΅Π΄Π»ΠΎΠΆΠ΅Π½ΠΈΠ΅ ΠΈ Π½Π΅ Π΄ΡƒΠ±Π»ΠΈΠΊΠ°Ρ‚ (*text = realloc(*text, ++nt * sizeof(char *)))[nt - 1] = str; str = 0; // Ρ‚Π΅ΠΏΠ΅Ρ€ΡŒ Ρ‚ΡƒΡ‚ Π±ΡƒΠ΄Π΅Ρ‚ Π²Ρ‹Π΄Π΅Π»ΡΡ‚ΡŒΡΡ новая ΠΏΠ°ΠΌΡΡ‚ΡŒ freestr = 0; // Π΄Π°ΠΆΠ΅ Ссли это послСднСС ΠΏΡ€Π΅Π΄Π»ΠΎΠΆΠ΅Π½ΠΈΠ΅, Ρ‚ΠΎ эту ΠΏΠ°ΠΌΡΡ‚ΡŒ Π½Π΅ Π½Π°Π΄ΠΎ ΠΎΡΠ²ΠΎΠ±ΠΎΠΆΠ΄Π°Ρ‚ΡŒ } } // для пустого прСдлоТСния Π±ΡƒΠ΄Π΅Ρ‚ использована ΡƒΠΆΠ΅ выдСлСнная ΠΏΠ°ΠΌΡΡ‚ΡŒ ls = 0; } } while (c != '\n' && c != EOF); if (freestr) free(str); *numberOfStr = nt; // Π²Π΅Ρ€Π½Π΅ΠΌ послСдний символ Π²Π²ΠΎΠ΄Π°, // ΠΏΠΎ Π½Π΅ΠΌΡƒ ΠΌΠΎΠΆΠ½ΠΎ ΠΎΠΏΡ€Π΅Π΄Π΅Π»ΠΈΡ‚ΡŒ ΠΊΠΎΠ½Π΅Ρ† Ρ„Π°ΠΉΠ»Π° // (Ссли Π² дальнСйшСм прСдполагаСтся Ρ‡ΠΈΡ‚Π°Ρ‚ΡŒ Π±ΠΎΠ»Π΅Π΅ ΠΎΠ΄Π½ΠΎΠΉ строки) return c; } 

It is clear that the name of the function reedAndSort with an error and does not quite correctly reflect its essence (the sort has nothing to do with it ...)