I was wondering if I can get some help on my program. As stated in the title, I'm getting an access violation in my program. I realize that it has something to do with dereferencing a garbage pointer, and I'm pretty sure I've narrowed down the line that its breaking on, I'm just not sure why it's actually breaking, and how to fix it. The code is as follows:
void determineInputType(char input[kMaxUserInput])
{
char* flag = NULL;
if (*(flag = strchr(input, '.')) != NULL)
{
double grade = 0;
if ((sscanf(input, "%lf", grade)) == 1) // problem is on this line
{
//rest of the code continues here
How I'm getting the input is here:
void getUserInput(char* userInput)
{
printf("Enter a student's grade: ");
fgets(userInput, kMaxUserInput, stdin);
clearCRLF(userInput);
}
and finally the main:
int main(void)
{
char userInput[kMaxUserInput] = { 0 };
getUserInput(userInput);
determineInputType(userInput);
return 0;
}
Any help on this would be greatly appreciated as it's been stumping me for a while. Thanks so much for your time, if there's anything else I need to post, let me know.
if (*(flag = strchr(input, '.')) != NULL)
... you are dereferencing the pointerflag
while it may beNULL
. You should change it intoif ((flag = strchr(input, '.')) != NULL)
– tiguchi