Hey all,
Any ideas on how to concatenate strings that are refered to by an array of pointers to pointers? This is what I have at the moment:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_NO_STR 10
#define MAX_LGT_STR 99
main(int argc, char *argv[]) {
char **strings;
int elements, i;
strings = malloc( MAX_NO_STR * sizeof(char *) );
if(strings == NULL) {
fprintf(stderr, "out of memory\n");
exit(1);
}
for(i = 0; i < MAX_NO_STR; i++) {
strings[i] = malloc(MAX_LGT_STR * sizeof(int));
if(strings[i] == NULL) {
fprintf(stderr, "out of memory\n");
exit(1);
}
}
if(argc != 3) {
strings[0]="Incorrect number of arguments for message ";
strings[1]= "argv[2] will be used here";
strings[2]=" generation";
elements=3;
catstrings(elements, strings);
exit(1);
}
for(i=0; i<MAX_NO_STR; i++) free(strings[i]);
free(strings);
exit(0);
}
catstrings(int elements, char **logmsg) {
int i;
for(i=1; i<elements; i++) {
strcat(logmsg[0], logmsg[i]);
}
}
The code compiles ok, but I get a segmentation fault each time I try to run it. I've tracked the error down to the line in the catstrings function that is supposed to perform the concatenation. Any ideas how I could achieve this or what the problem is?
Thanks in advance for your help.