I am attempting to write a program that will take two sets of strings N and Q. The goal of the program is to print out the number of times each string in Q occurs in N. However, I am struggling to manage strings and pointers in C and in particular I believe my issue stems from attempting to have an array of strings. I get a segmentation fault when the code below is executed. I have commented out my attempts at debugging using printf(). I believe the issue occurs when I try to assign S into the N_array.
int main() {
int N, Q;
char *N_array[1000], *Q_array[1000];
scanf("%d", &N);
for (int N_i = 0; N_i < N; N_i++) {
//printf("made it through A for loop %d times\n", N_i+1);
scanf("%s", N_array[N_i]);
}
scanf("%d", &Q);
//Does the array contain any information?
//for (int N_i = 0; N_i < N; N_i++) { printf("N_array[%d] == %d\n", N_i, N_array[N_i]);}
for (int Q_i = 0; Q_i < Q; Q_i++) {
//printf("Made it to B for loop\n");
int occurs = 0, result;
char s[21];
scanf("%s", &s[21]);
strcpy(Q_array[Q_i], s);
for (int N_i2 = 0; N_i2 < N; N_i2++) {
//printf("Made it to C for loop\n");
result = strcmp(Q_array[Q_i], N_array[N_i2]);
if (result == 0) occurs++;
}
printf("%d", occurs);
}
return 0;
}