digitalmars.D.learn - Access Violation
- Robert Jones (13/13) May 11 2006 Why am i getting an access violation:
- Chris Miller (7/20) May 11 2006 Your scanf is wrong; it takes a pointer to a float, not a real; or you c...
- David Medlock (5/56) May 11 2006 2 Problems I can see:
- Robert Jones (9/41) May 11 2006 Robert Jones said the following on 5/11/2006 1:35 PM:
Why am i getting an access violation: C:\Documents and Settings\jareth\Desktop>dmd -v -w ly2au.d parse ly2au semantic ly2au semantic2 ly2au semantic3 ly2au code ly2au generating code for function 'main' c:\dmd\bin\..\..\dm\bin\link.exe ly2au,,,user32+kernel32/noi; C:\Documents and Settings\jareth\Desktop>ly2au Enter number of light years: 5 Error: Access Violation Attached is the source code.
May 11 2006
On Thu, 11 May 2006 13:35:05 -0400, Robert Jones <robertjones21 HotPOP.com> wrote:Why am i getting an access violation: C:\Documents and Settings\jareth\Desktop>dmd -v -w ly2au.d parse ly2au semantic ly2au semantic2 ly2au semantic3 ly2au code ly2au generating code for function 'main' c:\dmd\bin\..\..\dm\bin\link.exe ly2au,,,user32+kernel32/noi; C:\Documents and Settings\jareth\Desktop>ly2au Enter number of light years: 5 Error: Access Violation Attached is the source code.Your scanf is wrong; it takes a pointer to a float, not a real; or you can use %lf for double*. double tempx; scanf("%lf", &tempx); x = tempx;
May 11 2006
Robert Jones wrote:Why am i getting an access violation: C:\Documents and Settings\jareth\Desktop>dmd -v -w ly2au.d parse ly2au semantic ly2au semantic2 ly2au semantic3 ly2au code ly2au generating code for function 'main' c:\dmd\bin\..\..\dm\bin\link.exe ly2au,,,user32+kernel32/noi; C:\Documents and Settings\jareth\Desktop>ly2au Enter number of light years: 5 Error: Access Violation Attached is the source code. ------------------------------------------------------------------------ import std.c.stdio; import std.stdio; // number of kilometers in a astronomical unit const real au = 148800000.0; // number of kilometers in a light year based on light second being exactly 300,000km const real ly = 9467280000000.0; real n; real x; real y; int main() in { assert(au is 148800000.0); // ensure that au is exactly 1,448,800,000km assert(ly is 9467280000000.0); // ensure that ly is exactly 9,467,280,000,000km } body { writef("Enter number of light years: "); scanf("%f", x); // convert light years to kilometers y = x * ly; // convert kilometers to astonomical units n = y / au; writefln("\nThe number of astronomical units is %f", n ); return 0; }2 Problems I can see: 1) scanf requires the address of a float, so use &x 2) initialize those reals to some value, or you will get nan -DavidM
May 11 2006
Robert Jones said the following on 5/11/2006 1:35 PM: Thanks Chris and David, below are the changes to the source which now compiles and does what it is supposed to do.import std.c.stdio; import std.stdio; // number of kilometers in a astronomical unit const real au = 148800000.0;const double au = 148800000.0;// number of kilometers in a light year based on light second being exactly 300,000km const real ly = 9467280000000.0;const double ly = 9467280000000.0;real n;double n = 0.0;real x;double x = 0.0;real y;double y = 0.0;int main() in { assert(au is 148800000.0); // ensure that au is exactly 1,448,800,000km assert(ly is 9467280000000.0); // ensure that ly is exactly 9,467,280,000,000km } body { writef("Enter number of light years: "); scanf("%f", x);scanf("%lf", &x);// convert light years to kilometers y = x * ly; // convert kilometers to astonomical units n = y / au; writefln("\nThe number of astronomical units is %f", n ); return 0; }
May 11 2006