What is the error in the code below. The idea is elementary - a string is entered, when read, the string is broken up into words, so that the output is an array [i] of words. The word delimiter is a space.

#include <stdio.h> #include <malloc.h> int main() { int i = 0; int j=0; char **words; //массив ΡƒΠΊΠ°Π·Π°Ρ‚Π΅Π»Π΅ΠΉ Π½Π° слова char c; //Π·Π°Π³ΠΎΡ‚ΠΎΠ²ΠΊΠ° ΠΏΠΎΠ΄ Ρ‡Ρ‚Π΅Π½ΠΈΠ΅ символов words = (char**)calloc(1,sizeof(char*)); //исходный Ρ€Π°Π·ΠΌΠ΅Ρ€ массива words[i] = (char*)calloc(1,sizeof(char));//исходный Ρ€Π°Π·ΠΌΠ΅Ρ€ слова printf("\n Enter the line... \n"); while ((c=getchar())!='\n') //считываСм посимвольно ΠΏΠΎΠΊΠ° Π½Π΅ наткнСмся Π½Π° ΠΊΠΎΠ½Π΅Ρ† строки { if (c!=' '){ //Ссли это Π½Π΅ ΠΏΡ€ΠΎΠ±Π΅Π», Ρ‚ΠΎ words[i]=(char*)realloc(words[i],(i+1)*sizeof(char)); //ΡƒΠ²Π΅Π»ΠΈΡ‡ΠΈΠ²Π°Π΅ΠΌ Ρ€Π°Π·ΠΌΠ΅Ρ€ Ρ‚Π΅ΠΊΡƒΡ‰Π΅Π³ΠΎ слова word[i] Π½Π° i+1 words[i][j] = c;//присваиваСм Ρ‚Π΅ΠΊΡƒΡ‰Π΅ΠΌΡƒ элСмСнту слова Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ считанного символа j++;//ΡƒΠ²Π΅Π»ΠΈΡ‡ΠΈΠ²Π°Π΅ΠΌ счСтчик Π½Π° 1 } else{//ΠΈΠ½Π°Ρ‡Π΅, words[i][j] = '\0';//присваиваСм послСднСму элСмСнту Π² словС символ ΠΊΠΎΠ½Ρ†Π° слова j=0;//обнуляСм счСтчик элСмСнтов Π² словС i++;//ΡƒΠ²Π΅Π»ΠΈΡ‡ΠΈΠ²Π°Π΅ΠΌ счСтчик слов Π½Π° 1 words = (char**)realloc(words,i*sizeof(char*)); //ΡƒΠ²Π΅Π»ΠΈΡ‡ΠΈΠ²Π°Π΅ΠΌ Ρ€Π°Π·ΠΌΠ΅Ρ€ массива слов Π½Π° 1 } ...//здСсь Π²Ρ‹Π²ΠΎΠ΄ слов "слово[1] .., слово [2]", Π½ΠΎ ошибка Π²Ρ‹ΡˆΠ΅ } getchar(); return 0; } 

When you enter one character, everything is fine (this character is output), if entered, three characters with a space (for example, "1 1") - an error: Program received signal SIGSEGV, Segmentation fault. -

  • Could I, unworthy person, ask the lowest question from the most respected author of the question, what happens during the execution of this ingenious program? And wouldn't the dear author be kind enough to add this to the above question? .. - user194374
  • Just trying to figure out how to do it in C. I have never encountered C before. - Alexander
  • An error in what is shown? - user194374
  • When entering 1 character, everything is good (this character is output), if entered, three characters with a space (for example, "1 1") - an error: Program received signal SIGSEGV, Segmentation fault. - Alexander
  • If you are given an exhaustive answer, mark it as correct (a daw opposite the selected answer). - Nicolas Chabanovsky ♦

1 answer 1

  1. Error in the string words[i]=(char*)realloc(words[i],(i+1)*sizeof(char)); . line size is stored in j, so you need ... (j+1)*sizeof ...
  2. Error in the string words = (char**)realloc(words,i*sizeof(char*)); . Since the i-index is the last word, the memory needs one more pointer - ... (i+1)*sizeof(char*) ...
  3. After that, under the new word you need to request a new memory: words[i] = (char*)calloc(1,sizeof(char));
  4. Well, nowhere is there a test for low memory (this is when calloc / realloc returns 0), and the program will end with an access error at the zero address.
  • one
    Thank you very much! Also, I have skipped a line with the addition of '\ 0' in the last word after exiting the loop. You are absolutely right about memory too, but this is for me the next step. - Alexander