I have an array of n strings where n is not known at compilation time.
The real input is a giant string that I will splice, and add the parts to each position of the array.
In the example I've simulated a sentence with n=3 , but n can be any number.
void addWords(char *array[][300], int n) {
char p[] = "Hello ";
char p1[] = "World ";
char p2[] = "!";
strcpy(array[0],p);
strcpy(array[1],p1);
strcpy(array[2],p2);
printf("%s%s%s\n",array[0],array[1],array[2]);
}
int main(int argc, char const *argv[])
{
int n = 3;
char array[n][300];
addWords(array,3);
return 0;
}
The code gives segmentation fault and I cannot identify the cause.
printf("%s%s%s\n",p[0],p[1],p[2]);you're passing the wrong arguments toprintf."%s"format specifier expects a string, but you're passing achar. Do you mean that to beprintf("%s%s%s\n",p0,p1,p2);?