I'm trying to write what I thought would be a simple program, but I'm running into some issues. When I try to read in a string to a char pointer, I get a segmentation fault, but only in certain parts of the code. I feel like it's an issue with the way I'm reading in the string, but I don't get any more information than segfault.
Here is my code:
#include <stdio.h>
#include <string.h>
int main() {
char *Q_or_U;
char *E_or_P;
int entry;
int rc;
while(1) {
printf("Would you like to query or update? (q/u): ");
rc = scanf("%s", Q_or_U);
if (rc != 1) break;
printf("received %s\n", Q_or_U);
if (strcmp(Q_or_U, "q") == 0 || strcmp(Q_or_U, "Q") == 0) {
//execution fine in this block
printf("Which entry would you like to query? ");
rc = scanf("%d", &entry);
if (rc != 1) break;
printf("received %d\n", entry);
}
else if (strcmp(Q_or_U,"u") == 0 || strcmp(Q_or_U, "U") == 0) {
//misbehaving
printf("Would you like to encrypt that message? (y/n): ");
rc = scanf("%s", E_or_P); //segmentation fault
if (rc != 1) break;
printf("received %s", E_or_P);
}
}
}
The segfault always occurs upon trying to read a string into my variable E_or_P, what am I missing? (I am new to C).