I'm making a program which sorts some input words according to some rules.
To allign them, I'd like to copy "words" to "tmp" by using memcpy to use and change "tmp".
I tried to declare tmp to double pointer or array, but the only I met was segmentation fault.
How could I copy all of "words"?
#include <malloc.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_WORD_LEN 30
int getInput(char*** words) {
int count;
int i;
char buffer[MAX_WORD_LEN+1];
printf("Enter the number of words: ");
scanf("%d", &count);
*words = malloc(count * sizeof(char*));
printf("Enter the words: ");
for (i = 0; i < count; i++) {
(*words)[i] = malloc((MAX_WORD_LEN + 1) * sizeof(char));
scanf("%s", buffer);
strcpy((*words)[i], buffer);
}
return count;
}
void solve() {
int count;
int i;
char ** words;
count = getInput(&words);
char ** tmp = malloc(count* sizeof(char*));
memcpy(tmp, words, sizeof(char *));
}
void main() {
solve();
return;
}