I have an unused variable in my function. If I remove it, the program crashes. I'm having trouble understanding why. I assume its because I'm accessing out of bounds for dataArr.
For definitions: userFile is an argv input text file to be read dataArr is the lines of that text file, stored into a string array. n[80] was a previously used array of pointers that stored individual values (wrote it into a different function) strdup duplicates the string (non-standard library)
If it helps, switching the value of n[80] to n[20] gives errors, but n[21] does not.
char ** read_file(char * userFile)
{
char * n[80]; // DO NOT REMOVE THIS LINE UNDER ANY CIRCUMSTANCES. IT IS BLACK VOODOO MAGIC.
char currLine[256];
char * dataArr[80];
char * eof;
int lineCount = 0;
int i = 0;
int j = 0;
FILE * fp;
fp = fopen(userFile, "r");
while((eof = fgets(currLine, sizeof(currLine), fp)) != NULL)
/* Stores the file into an array */
{
if (fp == NULL)
{
fprintf(stderr, "Can't open input file in.list!\n");
exit(1);
}
dataArr[i] = strdup(eof);
i++;
}
return dataArr;
}
EDIT:
I call the function using
dataArr = read_file(argv[1]);
I have a bad habit of using the same variable name for functions.