Below is the code for a method i made called inputReader it reads input from a textfile and copies it to a struct wordz and from that struct i retrieve the 3 most frequent words which is what is displayed below.
I am trying to concatenate all 3 words as one pointer so i can return it to the main method but whenever i use any method to do with w1, w2, w3, that copies to a new struct or array or pointer i ALWAYS get this error "Segmentation fault(core dumped)"
Any idea why this happens or how I can work around it?
Struct Code:
#define maxLetters 101
typedef struct {
char word[maxLetters];
int freq;
} WordArray; //struct type
Code:
char * w1; // most frequent word
char * w2; // second most frequent word
char * w3; // third most frequent word
// finds w1
for(j = 0; j < uniqueWords; j++)
if(wordz[j].freq == freqz[uniqueWords-2]+1)//excludes whitespace frequency
w1 = wordz[j].word;
// finds w2
for(j = 0; j < uniqueWords; j++)
if(wordz[j].freq == freqz[uniqueWords-3]+1)//excludes whitespace frequency
w2 = wordz[j].word;
// finds w3
for(j = 0; j < uniqueWords; j++)
if(wordz[j].freq == freqz[uniqueWords-4]+1)//excludes whitespace frequency
w3 = wordz[j].word;
char *p;
// if i dont include strcat methods the method runs fine and outputs fine
strcat(p, w1); // once this operation is executed i get the error
strcat(p, " ");
strcat(p, w2);
strcat(p, " ");
strcat(p, w3);