Trying to calculate the left most point in an Array of Points, the program blows up on me (segmentation fault (core dump) error).
Here's the interface:
//points.h
#define MAX_POINTS 100
struct Point {
char label;
int x;
int y;
};
int leftmostPoint(struct Point points[], int numPoints);
Here's the leftmostPoint implementation:
//points.c
//get the point with the smallest x value
int leftmostPoint(struct Point points[], int numPoints) {
int smallestX = points[0].x; //assume first point is smallest
int index;
for (int i = 1; i < numPoints; i++) {
if (points[i].x < smallestX) {
smallestX = points[i].x;
index = i;
}
}
return points[index];
}
Here's where the magic happens:
//magic.c
struct Point points[MAX_POINTS];
//build array via standard input (this works, tested by printing the points)
//only 5 points were added in
displayPoint(points[0]); //works
displayPoint(points[4]); //works
struct Point hull;
hull = leftmostPoint(points, numPoints); //this is where the program blows up
I am pretty sure it's an issue of sending pointers and not actual copies of the array (curse c!!), my question is where is the issue exactly and how can I go about fixing it?