#define HISTORY_SIZE 50
#define INPUT_SIZE 512 /*Max input size*/
char input[INPUT_SIZE]; /*Holding user input globaly*/
char* input_history[HISTORY_SIZE];
This is how im storing my input in to input, and wanting to store a copy of it in to input_history
void addToHistory()
{
/*input_history[currentHistorySize++] = strtok(input,"\n");*/
input_history[currentHistorySize++] = input;
printf("ADDEDTOHISTORY: %s \t\t %d \n", input_history[(currentHistorySize- 1)],currentHistorySize);
}
But when i go to print it out, it doesnt work ….
/*strcpy(input,input_history[currentHistorySize-2]);
printf("LAST INPUT, %s \n %s \n \n", input,input_history[currentHistorySize-2]);*/
printf("0: %s \n ", input_history[0]);
printf("1: %s \n ", input_history[1]);
printf("2: %s \n ", input_history[2]);
Ive been sitting trying to work this out for ages and cant seem to see where im going wrong, maybe a pair of new eyes will notice some silly mistake?
Basicly i want to take the users input using
fgets(input,INPUT_SIZE,stdin)
Then store a copy of it into char* input_history
And then be able to print it out later on.
Very simple.
The most likely problem is that you’re not actually copying the string, you’re just copying the pointer (the address of the string). Try this instead:
Or maybe:
You should also remember to free them when you’re done.