1
// Construct an array of length MAX_POINTS.
// Populate the array with given points and initialize the number points being handled.
// Print the given points for the user.
struct Point points[MAX_POINTS];
int numPoints = readPoints(points);
printf("Set of points: \n");
displayPoints(points, numPoints);

// Construct an array to represent the hull with max possible points (numPoints).
struct Point hull[numPoints];
struct Point pointOnHull = leftmostPoint(points, numPoints);
struct Point endPoint;
int e = 0;

// Perform Jarvis March.
do {
    hull[e] = pointOnHull;
    endPoint = points[0];
    for ( int i = 1; i < numPoints; i++ )
        if ( (endPoint == pointOnHull) || (ccw(hull[e], endPoint, points[i]) > 0) )
            endPoint = points[i];
    ++e;
} while ( endPoint != hull[0] );        // we've looped back to the beginning.

// Print the mapped hull points.
printf("Convex hull: \n");
displayPoints(hull, numPoints);

That's my code for a Jarvis March (gift wrapping) program to solve for convex hull. GCC is raising errors on both of my struct Point comparisons (endPoint == pointOnHull and endPoint != hull[0]). I'm brand new to C, and this is one of the first projects that I'm doing to play around with the language. Can anyone help me see where I'm messing up?

The specific errors:

jarvis.c:31:19: error: invalid operands to binary == (have 'struct Point' and 'struct Point')

jarvis.c:34:21: error: invalid operands to binary != (have 'struct Point' and 'struct Point')

Community
  • 1
  • 1
Nick
  • 53
  • 7
  • 1
    What is the type definition for `struct Point`? You might just need to do the comparison by parts, instead of relying on it to understand how to compare your structs. – Michael Oct 06 '13 at 20:18
  • Does C even provide struct comparison? As Michael said, you will have to do the comparison yourself. – Vaibhav Desai Oct 06 '13 at 20:20
  • Oh shit, that's a great point. I have an equals method in a header file that I was using for other stuff, but totally forgot about. I'll give that a shot. – Nick Oct 06 '13 at 20:20
  • @Michael +1 there isn't much "might" about it. He needs a `ComparePoint()s` function of some sort. – WhozCraig Oct 06 '13 at 20:20
  • http://www.google.com/search?q=C+compare+struct – Karoly Horvath Oct 06 '13 at 20:20
  • possible duplicate of [How do you compare structs for equality in C?](http://stackoverflow.com/questions/141720/how-do-you-compare-structs-for-equality-in-c) – Karoly Horvath Oct 06 '13 at 20:21

1 Answers1

2

There is no comparison operator for structs in C. You have to define your own comparison function and use it in place of ==. Try something like this:

bool equalPoints(const struct Point p1, const struct Point p2) {
    return (p1.x == p2.x) && (p1.y == p2.y);
}

if (equalPoints(endPoint, pointOnHull)) { /* then code here */ }

or if your structure is more complex (though, this is probably not the case):

bool equalPoints(const struct Point *p1, const struct Point *p2) {
    /* 
     * In case your structure is more complex here goes
     * more complex comparison code
     */
    return (p1->x == p2->x) && (p1->y == p2->y);
}

if (equalPoints(&endPoint, &pointOnHull) { /* then code here */ }

the latter won't copy whole Point structure to pass it to equalPoints, but will pass pointer (reference type) instead. const is important because it won't let you accidentally modify points when you only want to compare them.

zaquest
  • 2,030
  • 17
  • 25
  • 4
    +1 and note, depending on the complexity of the underlying structure it may be beneficial to pass-by-address rather than by-value. In this case, value is fine. Add a bunch of cruft into that structure and it could get expensive quick. Also, the params here can be `const`, but should definitely be so if passed by address. – WhozCraig Oct 06 '13 at 20:22
  • 1
    @VaibhavDesai it isn't as big a deal in this case, but should be done for by-address to be sure. – WhozCraig Oct 06 '13 at 20:25