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. -