2013-10-07 3 views
0

점의 배열에서 가장 왼쪽의 점을 계산하려고하면 프로그램이 나에게 불어납니다 (분할 오류 (코어 덤프) 오류).구조체 배열을 사용한 분할 오류

다음
//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!), 내 질문은 어디에 정확히 문제를 해결하고 어떻게 해결할 수 있습니까?

+0

죄송합니다. Jonathan, 스택 오버플로에서 코드를 작성했을 때 오타가되었습니다! 내 코드는 적절한 반환 값을 반영하고 leftmostPoint()는 실제로 구조체를 반환합니다. Point –

+0

leftmostPoint를 호출하면 numPoints를 전달합니다. 그 변수는 어디에 정의되어 있습니까? – Yoctohan

+0

죄송합니다. 사용자가 입력 한 점수를 읽은 후 numPoints가 먼저 정의됩니다. 이 예제에서 numPoints는 5입니다. –

답변

4

코드의 원래 버전에서 함수 leftmostPoint()int을 반환하지만 struct Point을 반환해야합니다. 컴파일러가 이에 대해 불평해야합니다. 는 (코드 이후 struct Point 돌아 업데이트되었습니다.)

호출 :

struct Point hull = leftmostPoint(points, numPoints); 

문제가 int 대신 struct Point을 반환한다 leftmostPoint()의 선언에 나타낸다.

struct Point (leftmostPoint(struct Point points[], int numPoints) 
{ 
    int smallestX = points[0].x; //take the first point in the list and assume it's smallest 
    int index = 0; 
    for (int i= 1; i < numPoints; i++){ 
     if (points[i].x < smallestX){ 
      smallestX = points[i].x; 
      index = i; 
     } 
    } 
    return points[index]; 
} 

또는 의해 :

그래서, 하나에 의해 해결

int leftmostPoint(struct Point points[], int numPoints) 
{ 
    int smallestX = points[0].x; //take the first point in the list and assume its smallest 
    int index = 0; 
    for (int i= 1; i < numPoints; i++){ 
     if (points[i].x < smallestX){ 
      smallestX = points[i].x; 
      index = i; 
     } 
    } 
    return index; 
} 

내 의심 int를 반환 버전이 더 유용하다는 것이다; 배열의 어느 항목이 항목의 값이 아니라 가장 왼쪽에 있는지 알아야합니다.

paxdiabloindex을 0으로 설정하면 배열의 첫 번째 항목이 가장 낮은 값인 x 값 인 경우 "임의"값을 반환하지 않을 수 있습니다.


당신이 컴파일 문제가 있었어야 고정 된 한 점을 감안, 다음 질문은 참으로 일해야합니다

  • 함수의 호출 numPoints의 가치는 무엇입니까

    ?

    struct Point (leftmostPoint(struct Point points[], int numPoints) 
    { 
        int smallestX = points[0].x; //take the first point in the list and assume it's smallest 
        int index = 0; 
        assert(numPoints > 0); 
        printf("-->> %s: numPoints = %d: index = %d, x = %d\n", 
          __func__, numPoints, index, smallestX); 
        for (int i= 1; i < numPoints; i++){ 
         if (points[i].x < smallestX){ 
          smallestX = points[i].x; 
          index = i; 
          printf("---- %s: index = %d, x = %d\n", __func__, index, smallestX); 
         } 
        } 
        printf("<<-- %s: index = %d: x = %d\n", __func__, index, points[index].x); 
        return points[index]; 
    } 
    

    을 또는 그 테마에 변형 :

당신은 항상 당신이 정확한 데이터를 받고 있다는 것을 확인하는 함수에 인쇄 코드를 추가 할 수 있습니다.

+0

Jonathan,'points [0]'가 x가 가장 작은 경우 무작위 값을 반환 할 가능성이 있기 때문에 루프 앞에 'index'를 초기화 할 수있는 기회를 얻었습니다. 정상적인 산문과 잘 어울리지 않는다면 내 말씨를 자유롭게 바꿀 수 있습니다. – paxdiablo

+0

@ paxdiablo : 감사합니다. 편집하는 동안 (또는 인덱스를 0으로 초기화하는 것에 대한 설명을 포함하여) 변경 사항을 반영했습니다. –

관련 문제