-1

Error in my code

Run Time Check Failure #3 - T

I tried many times to fix it, but I failed. I added pointer to x, y, but "Run Time Check Failure #3 - T" — same error. Can you help me to fix this error?

#include<stdio.h>
#include<math.h>    

typedef struct {
    double x, y;
}location;
double dist(location a,location b)
{
    return sqrt(pow(b.x - a.x, 2.0) + pow(b.y -a.y, 2.0));
}
void func(location l, location e)
{
    double z;
    location a = l;
    location b = e;
    printf("enter two dots:");
    scanf("%lf %lf", a.x, a.y);
    printf("enter two dots:");
    scanf("%1",a, b);
    printf("%.2lf", z);

}

void main()
{
    location l;
    location e;
    func(l, e);
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
ran barlev
  • 21
  • 1
  • 1
  • 4
  • 1
    `scanf("%1",a, b);`..look no further!! – Sourav Ghosh Apr 23 '17 at 15:43
  • 2
    @SouravGhosh: There's a need to look a little less far too: `scanf("%lf %lf", a.x, a.y);` is erroneous too — passing values and not pointers to `scanf()`. Plus the `printf("%.2lf",z)` is attempting to print an uninitialized value. The `dist()` function is unused so it is immaterial to the MCVE ([MCVE]). Passing uninitialized structures to `func()` is at best pointless and at worst treading on thin ice. And there's `void main()` which is only sort-of-OK if you're on Microsoft — see [What should `main()` return in C and C++](https://stackoverflow.com/questions/204476/). – Jonathan Leffler Apr 23 '17 at 16:15
  • BTW: simplification: `return sqrt(pow(b.x - a.x, 2.0) + pow(b.y -a.y, 2.0));` --> `return hypot(b.x - a.x, b.y -a.y);` – chux - Reinstate Monica Apr 23 '17 at 17:02

1 Answers1

0

The problems in the code were these:

1) scanf variable args must be passed as pointers. See scanf changes below.

2) initialise your variables in struct - thats the Run Time Check Failure #3 warning. see location initialisation below.

I also simplified a little. Hope that helps.

#include<stdio.h>
#include<math.h>    

typedef struct {
    double x, y;
}location;

double dist(location a, location b)
{
    return sqrt((b.x - a.x) * (b.x - a.x) + (b.y - a.y) * (b.y - a.y));
}

void main()
{
    location start = { 0 };
    location end = { 0 };
    printf("Enter start x, y co-ordinates: ");
    scanf("%lf %lf", &start.x, &start.y);

    printf("Enter end x, y co-ordinates: ");
    scanf("%lf %lf", &end.x, &end.y);

    printf("The distance between start and end: %lf\n", dist(start, end));
}
Angus Comber
  • 9,316
  • 14
  • 59
  • 107