This program has erformed an illegal operation. If the error persists...
I have recently started C on DEV-C++ recommended to me on a previous post. Still using the same old 1989 teach yourself C book though... Well everything is pretty simple so far but in a addition program an annoying message pops up halfway through the program, after entering numbers for addition etc.. Windows comes up with a message saying this program has performed an illegal operation and will be shut down etc... What is wrong?? The compiler found nothing wrong with the program!! I inserted a big arrow where the fault appears:
addition()
{
int num1;
int num2;
int answer;
char correct;
printf("\nenter a number ");
scanf("\n%d",num1);
printf("\nenter another number ");
scanf("\n%d",num2); <---------AFTER THIS BIT FAULT APPEARS
correct=''n'';
while(correct == ''n'')
{
printf("\n%d + %d = ",num1,num2);
scanf("\n%d",&answer);
if(answer==num1+num2)
{
printf("correct!");
correct=''y'';
}
else if(answer!=num1+num2)
{
printf("wrong!");
}
} //end of while
} //end of addition
Signed: ___T____
Signed: ___T____
you need to pass POINTERS to arguments to scanf, not the arguments themselves. So the function code should read something like:
int num2;
scanf("%d", &num2);
note the ampersand on num2 which ensures the ADDRESS of int num2 is passed.
int num2;
scanf("%d", &num2);
note the ampersand on num2 which ensures the ADDRESS of int num2 is passed.
The exception is when the variable you have is already a pointer itself, like when you an array of 20 chars called array (array[20]), and you do something like:
scanf("%s", array);
then the amperstand isn''t needed since ''array'' is a pointer by nature.
scanf("%s", array);
then the amperstand isn''t needed since ''array'' is a pointer by nature.
... and the reason why it crashes...
scanf wants the address of a variable. If you just pass num2, you pass the value in num2, not the address. So, scanf assumes the value in num2 is the address you want to store your number at. The value in num could be anything right now. 0, 1 or 13435211. scanf will store the number typed in at address 0, 1 or 13435211, which could be an area you''re not allowed to access, so windows says up yours and stops the program. (or at least that''s my take on it, not exactly sure what windows will do)
scanf wants the address of a variable. If you just pass num2, you pass the value in num2, not the address. So, scanf assumes the value in num2 is the address you want to store your number at. The value in num could be anything right now. 0, 1 or 13435211. scanf will store the number typed in at address 0, 1 or 13435211, which could be an area you''re not allowed to access, so windows says up yours and stops the program. (or at least that''s my take on it, not exactly sure what windows will do)
Woah, hold on a minute....
Shouldn''t that be %i, not %d? num2 is an int.
~~~~~~~~~~
Martee
http://www.csc.uvic.ca/~mdill
Shouldn''t that be %i, not %d? num2 is an int.
~~~~~~~~~~
Martee
http://www.csc.uvic.ca/~mdill
ReactOS - an Open-source operating system compatible with Windows NT apps and drivers
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement