I have tried accessing each word member from my struct array outside the while loop where I read input but it doesnt print anything.It seems like the array is empty but if I were to put that print statement in my while loop, it works fine.Not sure why this is the case.
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
struct wordArr {
char words[31];
int wordCount;
};
int main(void) {
const int ARRSIZE = 300;
char tmpWord[30] = " ";
char word[30] = " ";
// allocate memories for n number of struct wordarr.
// wordArrptr = (struct wordArr*) malloc(ARRSIZE * sizeof(struct wordArr));
struct wordArr **wordList = malloc(ARRSIZE * sizeof(struct wordArr *));
// const char delimiters[10] = " .,";
for (int i = 0; i < ARRSIZE ; i++) {
wordList[i] = malloc(sizeof(struct wordArr));
}
while (scanf("%s", tmpWord) != EOF) {
memset(word, ' ', 30);
int j = 0;
int i = 0;
for (int i = 0 ; i < strlen(tmpWord); i++) {
if (isalpha(tmpWord[i])) {
word[j] = tolower(tmpWord[i]);
j++;
}
}
strcpy(wordList[i]->words, word);
// printf("%s=>\n", wordList[i]->words);
wordList[i]->wordCount = 1;
i++;
}
for (int i = 0 ; i < ARRSIZE ; i++) {
printf("%s\n", wordList[i]->words);
}
return 0;
}