I wanted to use strcat()
to concatenate an element of an array of strings. I tried:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
char **str = malloc(sizeof(char *) * 3);
for (int i = 0; i < 3; i++) {
str[i] = malloc(sizeof(char) * 8);
}
str[0] = "foo";
str[1] = "bar";
strcat(str[0], "H");
for (int i = 0; i < 3; i++) {
printf("%s\n", str[i]);
}
free(str);
return 0;
}
and I get the error:
Segmentation fault (core dumped)
What should I do to get it right?