As stated in the title, I wanted to copy a string from a char pointer to location within an array of char pointers. When doing strcpy(), the output results in seg fault, but don't understand why this occurs.
The abbreviated code has the following:
void make_history(char *entry) {
//static char past_entries[10][10];
static char *past_entries[10];
static int index = 0;
static int array_index = 0;
char *input;
if((input = strchr(entry, '\n')) != NULL)
*input = '\0';
if(strcmp(entry, "history") != 0) {
strcpy(past_entries[index], entry);
*past_entries[index] = &entry;
index = (index + 1) % 10;
array_index++;
}
}
Instead of trying to return a 2d array (which is also very tricky), I thought it would be easier to copy the date from entry to the location within the array of pointers past_entries. Again, strcpy does not work, is there a valid reason as to why this occurs, and a possible workaround or solution to this fix?
Thank you
make_history. Read this: minimal reproducible examplemake_history("message")where "message is the string which I want to add to the array. There is not much to it.