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;
}
One problem is here
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]);
}
N_Array contains 1000 pointers to char, but each of those pointers points to, well.. nowhere. It's an uninitialized pointer which points to a random memory location you don't own. This is undefined behavior. You have to allocate memory before scanf.
N_Array[N_i] = malloc(max_length_of_string + 1);
Another problem is this line
char s[21];
scanf("%s", &s[21]);
The second parameter of scanf should be just s, not &s[21], which is just outside your array.
And one line below you have the same problem as described in my first point
strcpy(Q_array[Q_i], s);
Q_array[Q_i] doesn't yet point to any memory which you're allowed to write to. You should allocate memory here as well.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With