The problem is with the following statement -
strcpy(hist_array[history_counter], temp);
strcpy
tries to copy the buffer pointed to by temp
to the buffer pointed to by hist_array[history_counter]
but char *hist_array[20];
defines hist_array
to be an array of 20 pointers to characters. You should change your function to -
char *hist_array[20];
int history_counter = 0;
void save_to_history(char *temp) {
// +1 for the terminating null byte as strlen
// does not count the null byte
hist_array[history_counter] = malloc(1 + strlen(temp));
if(hist_array[history_counter] == NULL) {
printf("not enough memory to allocate\n");
// handle it
}
strcpy(hist_array[history_counter], temp);
printf("Saved %s to history to %d\n\n",
hist_array[history_counter],
history_counter);
history_counter++;
}