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);
}