I am trying to return an array of strings and while I copy the strings something weird happens when it passes the 4th index. For example, when it loops through the first 3 times it is stored as "the" but then it sudden becomes rewritten but it writes the next index just fine[index 5]. Can you guys find anything wrong with it because I'm stumped.
#include <stdlib.h>
#include <stdio.h>
#include "hash.h"
#include <string.h>
#define MAX 200
#define TERMINATE "asdfghjkl"
int createTable(int numFiles, char** files, char** stopList)
{
printf("stepped into create table\n");
FILE* fp1;
char oneWord[100];
HashTable hash = InitializeTable(900000);
int index = 2;
while(numFiles >0) {
fp1 = fopen(files[index++], "r");
while(fscanf(fp1, "%s", oneWord)!=EOF){
Insert(oneWord, hash, stopList);
}
numFiles--;
}
return 0;
}
char** createStopList(char* stopL)
{
FILE* fp1;
fp1 = fopen(stopL, "r");
char oneWord[100];
int i = 0;
char* stopList[MAX];
while(fscanf(fp1, "%s", oneWord)!=EOF){
stopList[i] = (char*)malloc(sizeof(oneWord));
strcpy(stopList[i++], oneWord);
}
stopList[i] = (char*)malloc(sizeof(char*));
strcpy(stopList[i], TERMINATE);
char** strings = stopList;
char** returnList = malloc(sizeof(strings));
i=0;
while(strcmp(strings[i], TERMINATE)!=0){
returnList[i] = malloc(sizeof(char*));
strcpy(returnList[i], strings[i]);
i++;
}
returnList[i] = (char*)malloc(sizeof(char*));
strcpy(returnList[i], TERMINATE);
return returnList;
}
int main(int argc, char** argv)
{
printf("start of prg\n");
char** stopList= createStopList(argv[1]);
createTable(argc-2, argv, stopList);
return 0;
}