The following method causes an error:
BOOL should_begin(void) {
char yn;
do {
printf("Continue? [Y/N] ");
yn = getchar();
printf("\n");
yn = toupper(yn);
if (yn == 'N') {
return FALSE;
}
} while (yn != 'Y');
return TRUE;
}
The code executes normally until toupper()
is reached, at which point there is a segmentation fault. I've seen questions like this where toupper()
was called on part of a string, but it was only when someone tried to modify a literal.
So what gives? char yn
shouldn't be read only, right? It's just a char
, a single byte of data, I'm not reading a whole string, am I?
EDIT:
This is my main()
function.
int main(int argc, char* argv[]) {
/* just some printf()s with instructions */
if (!should_begin()) {
return 0;
}
/* then continue with rest of the program */
return 0;
}