Thanks for your post, I have fixed my casting. But I still have my heap problem. I have also noticed that when 'inputCmd' finishes, the data is not sent back into 'main' though all the new pointers have been allocated (plus the usual extra ones as stated in my original post).
This is my new code. Any ideas?
Code:
void printData(char **data, int arrayLength, returnValue *condition)
{
int i = 0;
printf("Sorted Data:\n");
for(i = 0; i < (arrayLength - 1); i++)
printf(" %s,\n", data[i]);
printf(" %s.\n", data[arrayLength - 1]);
}
void terminate(returnValue *condition, char **data)
{
int i = 0;
int status = condition->status;
if(data != NULL)
{
for(i = 0; i < condition->returnValue[0]; i++)
{
if(data[i] != NULL)
free(data[i]);
}
if(data != NULL)
free(data);
}
if(condition != NULL)
{
if(condition->returnValue != NULL)
free(condition->returnValue);
free(condition);
}
if(status == OK)
exit(EXIT_SUCCESS);
else
exit(EXIT_FAILURE);
}
void inputCmd(char **data, returnValue *condition)
{
int i = 0;
int arrayLength = 0;
printf("How Many Elements Are You Entering? ");
fscanf(stdin, "%d", &arrayLength);
/*Read in last '\n' from stdin to empty out the stream*/
getc(stdin);
if(arrayLength <= 0)
{
printf("Could Not Read Data.\n");
condition->status = NOREAD;
}
else
{
data = realloc(data, sizeof(char *) * arrayLength);
for(i = 0; i < arrayLength; i++)
{
data[i] = calloc(wordSize, sizeof(char));
printf("Enter Data (One At A Time): ");
fscanf(stdin, "%s", data[i]);
data[i] = realloc(data[i], sizeof(char) * (strlen(data[i]) + 1));
}
sprintf(condition->returnValue, "%d", arrayLength);
}
}
int main(int argc, char* argv[])
{
FILE *dataFile = NULL;
char **data = calloc(1, sizeof(char *));
char inputLocation[wordSize], fileLocation[wordSize];
int arrayLength = 0;
returnValue *condition = calloc(1, sizeof(returnValue));
condition->returnValue = calloc(15, sizeof(char));
memset(inputLocation, '\0', wordSize);
memset(fileLocation, '\0', wordSize);
do
{
condition->status = OK;
askInputLocation(inputLocation);
checkInputLocation(inputLocation, condition);
if(condition->status == OK)
{
switch(inputLocation[0])
{
case 'c':
{
inputCmd(data, condition);
arrayLength = (int)condition->returnValue[0];
break;
}
default:
break;
}
}
if(condition->status == OK)
printData(data, arrayLength, condition);
}
while(condition->status != OK);
sprintf(condition->returnValue, "%d", arrayLength);
terminate(condition, data);
/*
* This code should never be run, therefore,
* 'EXIT_FAILURE' is sent back if it is.
*/
return(EXIT_FAILURE);
}