c - Segmentation fault with struct array -


trying calculate left point in array of points, program blows on me (segmentation fault (core dump) error).

here's interface:

//points.h #define max_points 100  struct point {    char label;    int x;    int y; };  int leftmostpoint(struct point points[], int numpoints); 

here's leftmostpoint implementation:

//points.c //get point smallest x value int leftmostpoint(struct point points[], int numpoints) {    int smallestx = points[0].x; //assume first point smallest    int index;    (int = 1; < numpoints; i++) {       if (points[i].x < smallestx) {          smallestx = points[i].x;          index = i;       }    }    return points[index];  } 

here's magic happens:

//magic.c struct point points[max_points]; //build array via standard input (this works, tested printing points) //only 5 points added in displaypoint(points[0]); //works displaypoint(points[4]); //works  struct point hull;  hull = leftmostpoint(points, numpoints); //this program blows 

i pretty sure it's issue of sending pointers , not actual copies of array (curse c!!), question issue , how can go fixing it?

in original version of code, function leftmostpoint() supposed return int return struct point. compiler should complaining this. (the code has since been updated return struct point.)

the invocation:

struct point hull = leftmostpoint(points, numpoints); 

indicates problem in declaration of leftmostpoint(), should returning struct point instead of int.

so, fix either by:

struct point (leftmostpoint(struct point points[], int numpoints) {     int smallestx = points[0].x; //take first point in list , assume it's smallest     int index = 0;     (int i= 1; < 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 first point in list , assume smallest     int index = 0;     (int i= 1; < numpoints; i++){         if (points[i].x < smallestx){            smallestx = points[i].x;            index = i;        }     }     return index; } 

my suspicion version returning int more useful; need know entry in array left-most, rather value of entry.

you'll note paxdiablo set index 0 avoid possibility of returning "random" value if first item in array 1 lowest x value.


given you've fixed should have been compilation problems, next question should indeed be:

  • what value of numpoints in call function?

you can add printing code function check you're getting correct data:

struct point (leftmostpoint(struct point points[], int numpoints) {     int smallestx = points[0].x; //take first point in list , assume it's smallest     int index = 0;     assert(numpoints > 0);     printf("-->> %s: numpoints = %d: index = %d, x = %d\n",            __func__, numpoints, index, smallestx);     (int i= 1; < 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 theme.


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -