2

If I have a struct like this:

struct Point 
{
    double x;
    double y;
};

and a function like this

double geometricDistance(struct Point point1, struct Point point2)
{
    double xDist = point1.x - point2.x;
    double yDist = point1.y - point2.y;
    return sqrt((xDist * xDist) + (yDist * yDist));
}

when I call geometricDistance, I can't just do geometricDistance(5,6); or something like that, because that's only two integers. How do I call it?

Thanks.

Oh yeah it's in C by the way.

Got it- use braces. Thanks.

Josh
  • 61
  • 6

3 Answers3

3

So you have your struct.

In main you can declare a point.

struct Point point1;
struct Point point2;

Now you made a struct variable called point that has access to both of those values double x and double y in your struct.

point1.x = 12;
point1.y = 15;

point2.x = 5;
point2.y = 6;

To pass it into the function you pass in a pointer to the struct, that lets you edit the value of that point. //function call double value = geometricDistance(&point1, &point2);

double geometricDistance(struct Point* point1, struct Point* point2)
{
  double xDist = point1->x - point2->x;
  double yDist = point1->y - point2->y;
 return sqrt((xDist * xDist) + (yDist * yDist));
}

Edit: I realized that you don't actually need to pass in the pointer to the struct. you can simply make the function parameters double geometricDistance(struct Point point1, struct Point point2) since you're not changing the values of any of the struct variables you declared. Your function call could simply be double value = geometricDistance(point1, point2); Inside of the function instead of using the -> reference you can use the . reference like this point1.x or point1.y

Enigma
  • 373
  • 2
  • 10
  • 1
    the value returned by `geometricDistance` is a `double` not an `int`. –  Apr 23 '16 at 21:39
  • Makes sense. Thanks. I see now why pointers are used with functions and structs. – Josh Apr 23 '16 at 21:40
  • 1
    You have also changed the signature of the function. The arguments to pass are `struct Point`, not `struct Point *`. If you want to use the pointers you have to change the body of the function, for example you have to use `point1->x` instead of point1.x –  Apr 23 '16 at 21:40
  • @GerardoZinno Thanks fixed it. – Enigma Apr 23 '16 at 21:43
  • @Josh If my answer helped you solve your problem would you mind checking the check mark next to my post? – Enigma Apr 23 '16 at 22:45
1

You have to pass a variable of type struc Point.

For example:

struct Point A;
struct Point B;
//add values in the fields x and y if you need to
.
.
.
double something = geometricDistance(A, B);
0

There are two other categories that you should know about that are also referred to as "parameters". They are called "parameters" because they define information that is passed to a function.

Actual parameters are parameters as they appear in function calls.
Formal parameters are parameters as they appear in function declarations.

The definition/prototype of function geometricDistance shows that it needs two parameters of type struct Point. You just need to call geometricDistance function with two parameter of type struct point.

For example,

struct point a,b;
.
.
double result = geometricDistance(a,b);
abhiarora
  • 9,743
  • 5
  • 32
  • 57