0

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

3
  • 2
    Please show how you call make_history. Read this: minimal reproducible example Commented Sep 26, 2018 at 12:29
  • I mean, the call to the function is just make_history("message") where "message is the string which I want to add to the array. There is not much to it. Commented Sep 26, 2018 at 12:45
  • You should mention this in your question. Commented Sep 26, 2018 at 12:46

1 Answer 1

1

In your example past_entries is just an array of pointers, but you don't allocate any memory to them (initially pointing to NULL). You then try to write to those locations, hence the crash.

To solve this, just allocate some memory before you try to copy your string to it:

past_entries[index] = malloc(strlen(entry) + 1);

Of course you should not forget to free all of this in the end.

Oh and, remove that line: *past_entries[index] = &entry;. It tries to assign a pointer to a character array to a character.

Sign up to request clarification or add additional context in comments.

6 Comments

Since past_entries is declared static, it is in fact guaranteed the elements will initially be null pointers. Still bad news.
@aschepler Thank you, I've fixed that part of the answer.
@Qubit I wanted to have the values be static as I want the array to update for each call to the function. The general idea is to create a circular array that shows that last 10 entries made -- kind of a like a history
I think you misunderstand what this does. Sure, you can have a static array of 10 character pointers and the addresses will be saved between function calls. But you still need them to point somewhere meaningful for them to store data (your entries). You can either do that with the line you have commented out (if you know an upper bound to the size of the entries) or dynamically using malloc (if you do not know an upper bound).
@Qubit I guess I just am having trouble understanding how this new pointer is returned
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.