Scanf produces segmentation fault for non-pointer argument

南楼画角 提交于 2020-02-25 08:13:39

问题


I am writing a program in c that outputs the html file, actually I am learning CGI programming. This program stops at run time when it executes the loop ('for' and 'while' both).

The error returned is Segmentation fault (core dumped)

The code(only the segment of program that is causing the problem) is:

void main()
{
int track=0;
int question_no;
printf("\nHow many questions?\t");
scanf("%d",question_no);
while(track<=question_no)
    {
        printf("\nAshish");
        track++;
    }
}

Actually I get the answer: I should use '&' when inputting value into question_no.

But my next question is WHY? should I use &? Because I defined question_no as int only not int pointer.


回答1:


Why should I use &? Because I defined question_no as int only not int pointer

That would be why. scanf needs a pointer in order to mutate the value. It expects a pointer, you pass it an int which is converted to a pointer which has some garbage value (address).




回答2:


Change

scanf("%d",question_no);

to

scanf("%d",&question_no);

and see what happens...

scanf takes a pointer to the memory location where to put the int value. So it tried to use question_no as an address and that caused the seg fault. question_no at that point had never been initialized so it could have been anything and the program tried to write to that location.

The & returns the address of question_no.




回答3:


Basically a segmentation faut is when your program attempts to access memory that it has not been assigned. You particular issue is this line, which invokes undefined behavior since you are using question_no uninitialized here:

scanf("%d",question_no);

and you are treating question_no as if it was a pointer, this is what you want:

scanf("%d",&question_no);

Enabling warnings would probably have helped you spot this error, gcc provides the following warning:

warning: format ‘%d’ expects argument of type ‘int *’, but argument 2 has type ‘int’ [-Wformat]

For gcc I often use -Wall -Wextra -pedantic and you can find a complete description here. Some documents for scanf may also be helpful.




回答4:


scanf is a C function, and since C doesn't have 'references' like C++ has, passing a pointer to scanf is the only way scanf is able to store its results. If scanf doesn't have a pointer telling it where to store the result, then there is no way it can give its result back to the caller.

Thus scanf is written to accept (and expect) pointers as its arguments, and if you give it int's instead of pointers it will simply attempt to use those int's as pointers - which will likely cause a seg-fault.




回答5:


'scanf()' stores values, so it needs a place to store them.This is done by providing the addresses of where to store the values.'&' is used to refer addresses of variable in scanf statement.



来源:https://stackoverflow.com/questions/20062787/scanf-produces-segmentation-fault-for-non-pointer-argument

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!