I need help with allocating memory to my variables. I'm looking for specific lines that are causing the crash and how to fix them. I've tried reallocating and malloc in many places and in different ways. I have a rough idea about which lines are problematic (my realoc lines?).
int main() {
char * myString;// myString uesd to hold users input as entered
myString = input1(); //Get users input and stor it in myString
int sentence_count = 0; //To keep track of the total number of lines entered
sentence * sentences; //array of sentences to hold all the sentences as an instance of sentence
sentences = NULL;//initialize all values
myprint(sentence_count, sentences, myString);//Print word and lines
search(myString, sentence_count, sentences);//Search for word in sentences
free(myString);//free the memory set aside
return 0;
}
char * input1() {
char * line;// temporary variable to one hold input line
char * myString;//myString uesd to hold users input as entered
line = (char * ) malloc(1024 * sizeof(char));
myString = malloc(1024);
myString[0] = '\0';
line[0] = '\0';
while (line[0] != '\n') {
printf(" Please input a line : ");
fgets(line, 1000, stdin);
strcat(myString, line);//add line to mystring
}
free(line);
return myString;
}
I run it on Linux on PowerShell and get the following error
Segmentation fault (core dumped)