试图计算点数组中最左边的点,程序炸毁了我(分段错误(核心转储)错误)。
这是界面:
//points.h
#define MAX_POINTS 100
struct Point {
char label;
int x;
int y;
};
int leftmostPoint(struct Point points[], int numPoints);
这是leftmostPoint
实现:
//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];
}
这就是神奇发生的地方:
//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
我很确定这是发送指针而不是数组的实际副本的问题(诅咒c !!),我的问题是问题到底出在哪里,我该如何解决?