0

I've been struggling with this for a stupidly long amount of time. Basically, I need to copy an array of char pointers to another array of char pointers.

Right now, I have the function:

void copyArray(char *source[], char *destination[]) {
    int i = 0;

    do {
        destination[i] = malloc(strlen(source[i]));
        memcpy(destination[i], source[i], strlen(source[i]));
    } while(source[i++] != NULL);
}

This results in a segmentation fault. Could someone please help?

Thanks!

EDIT: sample program

#include <string.h>
#include <stdio.h>
#include <stdlib.h>

// Copy the contents of one array into another
void copyArray(char *source[], char *destination[]){
    // printf("In copy array");
    int i = 0;

    do {
        destination[i] = malloc(strlen(source[i]));
        memcpy(destination[i], source[i], strlen(source[i]));
    } while(source[i++] != NULL);
}

void addToHistory(char *history[][40], char *args[]){
    int i;
    for(i = 1; i < 10; i++){
        copyArray(history[i], history[i-1]);
    }
    i = 0;
    copyArray(args, history[0]);
}

int main(void){
    char *history[10][40];
    char *args[40];

    history[0][0] = NULL;

    args[0] = "ls";
    args[1] = NULL;

    addToHistory(history, args);
}
5
  • are you certain that the array source[] has a final NULL value? Commented Jan 22, 2014 at 17:52
  • Did you try using a debugger? Commented Jan 22, 2014 at 17:53
  • Can you show a full (but small) example program which demonstrates this problem? Commented Jan 22, 2014 at 17:53
  • For my purposes, I do know that source will end with a final NULL value (it is the way the string is parsed in my program). I am working on a simple shell program and I uploaded a small test-code snippet.. Commented Jan 22, 2014 at 17:59
  • Please check the answer for your qn, here : stackoverflow.com/questions/36565328/… Commented Apr 12, 2016 at 15:54

3 Answers 3

1
  1. Make sure that the last element in the source array is NULL, before you pass it to copyArray.

  2. In copyArray, put the while instead of the do, and increment i only at the end of the loop.

Instead all of the above, you can simply change i++ to ++i in function copyArray.

But it will crash if the first element in the source array passed to this function is NULL.

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

Comments

0

I think you have an off-by-one error:

do {
    destination[i] = malloc(strlen(source[i]));
    memcpy(destination[i], source[i], strlen(source[i]));
} while(source[i++] != NULL);
               ^^^

You check if i was NULL after you already used it, and then end the loop. Try replacing it with

} while (source[++i] != NULL);           // or while (source[++i]), for short

You can try logging a short message after each iteration to see where the code faults.

Edit: Is there a reason you are using memcpy() (which will not copy the terminating '\0') rather than strcpy() (which will)?

(Note to @wildplasser: I believe strdup() may not be standard C).

Comments

0
void copyArray(char *source[], char *destination[]) {

    while ((*destiantion = *source)) {
        *destination++ = strdup( *source++ );
    }
}

BTW: it is common to make destination the first argument, just as in strcpy()

void copyArray(char *destination[], char *source[]) { ... }

Comments

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.