2013-10-07 43 views
0

試圖計算點陣列中最左邊的點,程序在我身上爆炸(分段錯誤(核心轉儲)錯誤)。結構數組的分段錯誤

這裏的接口:

//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 

我敢肯定它的發送指針的問題而不是數組的實際副本(curse c !!),我的問題是在哪裏問題確切,我該如何解決它?

+0

對不起Jonathan,當我在Stack Overflow中編寫代碼時,這是一個錯字!我的代碼反映了適當的返回值,leftmostPoint()的確返回了一個結構體Point –

+0

當你調用leftmostPoint時,你傳遞了numPoints。該變量在哪裏定義? – Yoctohan

+0

對不起,numPoints是在我讀取用戶輸入了多少點之後在主體中定義的。在這個例子中,numPoints是5. –

回答

4

在代碼的原始版本,你的函數leftmostPoint()本來是要返回int但你返回struct Point。編譯器應該抱怨這個。 (該代碼已經被更新,以返回一個struct Point。)

的調用:

struct Point hull = leftmostPoint(points, numPoints); 

指示問題是在leftmostPoint()聲明,應返回struct Point代替int的。

所以,無論是固定的:

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是比較有用的;您需要知道數組中的哪個條目是最左邊的,而不僅僅是條目的值。

您還會注意到paxdiablo設置index爲零,以避免如果數組中的第一項是一個具有最低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,我已經有機會在循環前初始化'index',因爲如果'points [0]'具有最小的x,它可能會返回一個隨機值。如果它與你的正常散文不吻合,請隨意更改我的措辭。 – paxdiablo

+0

@paxdiablo:謝謝 - 我在編輯時加入了您的更改(或同等功能)(包括將索引初始化爲0的註釋)。 –