It still crashes.
Here's the entire code with all the headers and other info, just in case it might be of interest. The other code isn't producing a problem, however, because the program doesn't crash unless the displayWords() function is executed.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#define STRING_MAX 102
#define FALSE 0
#define TRUE 1
#define ARTICLE 0
#define NOUN 1
#define VERB 2
#define PREPOSITION 3
char **preps;
char **nouns;
char **verbs;
char **arts;
int numWords[4];
int choice[3];
char inputFileStr[STRING_MAX];
FILE * inputFile;
void allocateWords(char ** words, int type)
{
int size_x = numWords[type];
//This will be determined by the word culling.
int size_y = STRING_MAX;
// memset(numWords, 0, 4);
int *a;
int i=0;
if ( (words = (char **)malloc(size_x * sizeof(char *)) ) == NULL )
{
printf("\nError, memory not allocated.\n");
exit(1);
}
for (i = 0; i < size_x; i++)
*(words+i) = (char *)malloc(size_y * sizeof(char));
//Now fill the words in the dynamically allocated array;
//a[0] = "what";
//a[1] = "cool";
}
void addWord(char ** words, int type, char word[])
{
numWords[type]++;
words = (char **) realloc(words, numWords[type]);
*(words+(numWords[type]-1)) = word;
}
void displayWords(char ** words, int type)
{
int i;
if((words+0) == NULL) printf("It's null");
else
printf("%s", (words+0));
/**//*
for(i=0; i<numWords[type];i++)
{
printf("%s ", words[i]);
}
*/
}
void displayMainMenu()
{
printf("\n ");
printf("\nPlease choose from the following: ");
printf("\n1) Generate a Sentence ");
printf("\n2) Display all words ");
printf("\n3) Add a word ");
printf("\n4) Delete a word ");
printf("\n5) Display all words sorted ");
printf("\n6) Exit the program");
printf("\nChoice --> ");
}
void getChoice(int numOptions, int * tempChoice)
{
scanf("%i", tempChoice);
while(*tempChoice<1 || *tempChoice>numOptions)
{
printf("\n\nEnter a correct choice --> ");
scanf("%i", tempChoice);
}
}
void cullFile(FILE * iFile)
{
}
char ** test;
int main(int argc, char *argv[])
{
memset(choice, 0, 3);
memset(numWords, 0, 4);
allocateWords(preps, PREPOSITION);
addWord(preps, PREPOSITION, "Yours");
//displayWords(preps, PREPOSITION);
printf("%s",*preps);
/*
**test = 'c';
printf("%c", **test);
*/
printf("Welcome to the Random Sentence Generator \n\n");
printf("Please enter the name of the input file: ");
scanf("%s",inputFileStr);
while((inputFile = fopen(inputFileStr, "r")) == NULL)
{
printf("\n\nPlease enter the name of the input file: ");
scanf("%s",inputFileStr);
};
//cullFile(inputFile);
while(choice[0] != 6)
{
displayMainMenu();
getChoice(6, &choice[0]);
if(choice[0] == 1)
{
// generateSentence();
}
else if(choice[0] == 2)
{
// displayAllWords();
}
else if(choice[0] == 3)
{
// addAWord();
}
else if(choice[0] ==4)
{
// deleteAWord();
}
else if(choice[0] ==5)
{
// displayAllSorted();
}
else if(choice[0] ==6)
break;
/*
scanf("%i", &choice[0]);
while(choice[0]<1 || choice[0]>6)
{
printf("\n\nEnter a correct choice --> ");
scanf("%i", &choice[0]);
}
*/
}
printf("Thank you and have a nice day. Goodybe. :) \n\n");
system("PAUSE");
return 0;
}