점의 배열에서 가장 왼쪽의 점을 계산하려고하면 프로그램이 나에게 불어납니다 (분할 오류 (코어 덤프) 오류).구조체 배열을 사용한 분할 오류
다음//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];
}
마법이 일어나는 것 :
여기//points.h
#define MAX_POINTS 100
struct Point {
char label;
int x;
int y;
};
int leftmostPoint(struct Point points[], int numPoints);
가 leftmostPoint
구현의 : 여기
//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!), 내 질문은 어디에 정확히 문제를 해결하고 어떻게 해결할 수 있습니까?
죄송합니다. Jonathan, 스택 오버플로에서 코드를 작성했을 때 오타가되었습니다! 내 코드는 적절한 반환 값을 반영하고 leftmostPoint()는 실제로 구조체를 반환합니다. Point –
leftmostPoint를 호출하면 numPoints를 전달합니다. 그 변수는 어디에 정의되어 있습니까? – Yoctohan
죄송합니다. 사용자가 입력 한 점수를 읽은 후 numPoints가 먼저 정의됩니다. 이 예제에서 numPoints는 5입니다. –