I am working on a vocabulary training program where you can add, practice, view and change your vocabulary. For each word in english (max 50 characters), I want to have a swedish translation/definition (max 999 characters) and a few example sentences (max 10, each containing max 999 characters). For this I used a structure named Word, and since I want more than one word I made an array of type Word with a size of 100 (max 100 words). This worked fine until I created a function in which I want to declare a new temporary array of type Word (because I will randomize the order and I do not want the first array to be affected). I set the size to MAXSIZE (100) but then entire program terminated. After some experimenting I found that the program works when I set the size of the temporary array to 92, but does not work if I set it to 93. Why is this? Does it have to do with my array taking up too much memory? I am quite new to c programming and hence a simple explanation would be great. Code is below: Thank you.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define MAXWORDS 92
#define MAXEWORD 50
#define MAXSENTENCE 999
#define MAXNR 10
typedef struct {
char eWord[MAXEWORD];
char sWord[MAXSENTENCE];
char sentence[MAXNR][MAXSENTENCE];
} Word;
void printEnglishWords(Word theWords[], int thePos) {
for(int i=0; i<thePos; i++) {
printf("%s, ", theWords[i].eWord);
}
}
void shuffleWords(Word theWords[], int thePos) {
Word wordTemplate[MAXWORDS];
Word hold;
for(int i=0; i<thePos; i++) {
wordTemplate[i]=theWords[i];
}
for(int i=0; i<thePos; i++) {
int index=rand()%thePos;
hold=wordTemplate[i];
wordTemplate[i]=wordTemplate[index];
wordTemplate[index]=hold;
}
printf("\n");
printEnglishWords(wordTemplate, thePos);
}
int main(void) {
srand((unsigned) time(NULL));
Word myWords[MAXWORDS];
int pos;
myWords[0]=(Word){"Dog", "Hund", {"I own a dog"}};
myWords[1]=(Word){"Cat", "Katt", {"I do not own a cat"}};
myWords[2]=(Word){"Food", "Mat", {"I am hungry and I need some food"}};
pos=3;
printEnglishWords(myWords, pos); //output is Dog, Cat, Food,
shuffleWords(myWords, pos); //output is Food, Dog, Cat,
return 0;
}
When MAXWORDS is 92 the output is written above. However, when MAXWORDS is 93 only "Dog, Cat, Food, " is written, and the shuffleWords function does not work.