[英]Segmentation fault with struct array
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: 这是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];
}
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? 我很确定这是发送指针的问题,而不是数组的实际副本(curse c !!),我的问题是问题到底在哪里,我该如何解决?
In the original version of the code, your function leftmostPoint()
was supposed to return an int
but you return a struct Point
. 在代码的原始版本中,您的函数leftmostPoint()
应该返回一个int
但是您返回一个struct Point
。 The compiler should be complaining about this. 编译器应该对此抱怨。 (The code has since been updated to return a struct Point
.) (此后,代码已更新为返回struct Point
。)
The invocation: 调用:
struct Point hull = leftmostPoint(points, numPoints);
indicates the problem is in the declaration of leftmostPoint()
, which should be returning a struct Point
instead of an int
. 指示问题出在leftmostPoint()
的声明中,该声明应该返回struct Point
而不是int
。
So, fix either by: 因此,请通过以下任一方法修复:
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];
}
Or by: 或通过:
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;
}
My suspicion is that the version returning the int
is more useful; 我怀疑返回int
的版本更有用。 you need to know which entry in the array was the left-most, rather than just the value of the entry. 您需要知道数组中的哪个条目是最左边的,而不仅仅是条目的值。
You'll also note that paxdiablo set index
to zero so as to avoid the possibility of returning a "random" value if the first item in the array is the one with the lowest x
value. 您还将注意到paxdiablo将index
设置为零,以避免数组中的第一项是x
值最低的项时返回“随机”值的可能性。
Given that you've fixed what should have been compilation problems, the next question should be indeed be: 既然您已经解决了编译问题,那么下一个问题确实应该是:
numPoints
in the call to the function? 函数调用中numPoints
的值是什么? You can always add printing code to a function to check that you're getting correct data: 您始终可以将打印代码添加到函数中,以检查是否获取了正确的数据:
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];
}
Or variants on that theme. 或该主题的变体。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.