[英]C++: 0xC0000005: Access violation writing location 0x00000000
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. 非常感谢您抽出宝贵的时间,如果还有其他需要发布的信息,请告诉我。
The scanf
family requires you to supply a pointer to the value you want populated: scanf
系列要求您提供一个指向要填充的值的指针 :
if ((sscanf(input, "%lf", &grade)) == 1) // problem is no longer on this line
The way you have it, you set grade
to zero then use that as the pointer. 你有它的方式,可以设置grade
为零,那么使用它作为指针。
The relevant part of the standard (C99 on which C++11 defers to) is 7.19.6.2 /12
(my bold): 标准的相关部分(C ++ 11 7.19.6.2 /12
C99)为7.19.6.2 /12
(我的粗体):
a,e,f,g
- Matches an optionally signed floating-point number, infinity, orNaN
, whose format is the same as expected for the subject sequence of thestrtod
function.a,e,f,g
匹配一个可选的带符号浮点数,无穷大或NaN
,其格式与strtod
函数的主题序列所期望的格式相同。 The corresponding argument shall be a pointer to floating. 相应的参数应为指向浮动的指针。
l
(ell) - Specifies that ... a followinga
,A
,e
,E
,f
,F
,g
, orG
conversion specifier applies to an argument with type pointer to double;l
(ell)-指定...后面的a
,A
,e
,E
,f
,F
,g
或G
转换说明符适用于类型指针为double的参数; ... ...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.