segmentation fault in quick-sorting function

I was hoping someone could help me point out what is wrong in my program as I have a hard time figuring out. So basically I have a structure Location and a data file that gives fields separated by "|", for instance 1|VA|12345|New York|... (20 fields in total every line, the first field is a unique ID but they're out of order, some fields are optional [blank] but the first field will always be there)

struct _Location{
char** array; };

int compareLoc(const void *left, const void *right){
const Location *l = (Location*) left;
const Location *r = (Location*) right;
if (l->array[0]<r-> array[0]){return -1;}
else if (l->array[0]==r-> array[0]){return 0;}
else{return 1;}

}
int main(int argc, char* argv[]){
FILE* GISfile = fopen(argv[1], "r");
typedef struct _Location Location;
Location l[argv[2]]; //the size is given as a command
int count =0;
while (1){ l[count].array = malloc(20*sizeof(char*));
l[count].array[0] = malloc(121*sizeof(char)); //max data length of 121
if (fscanf(GISfile, "%[^|]s", l[count].array[0]==1){
for (int i=1; i<20; i++){
l[count].array = malloc(121*sizeof(char));
}
}
else {break;}
}

Location* point = l; qsort (l, size,sizeof(Location), compareLoc);
}

any insight? I know it is probably something to do with the way I'm reading in the data but I can't figure out why it would be inaccessable since I ensure that each spot is allocated... (perhaps it has to do with understanding the professor's given "size" and getting rid of null entries if size is generously too big, but I would like to make sure I haven't made any other logic errors up to this point)
 

cburn11

Ars Praetorian
447
Subscriptor
I don't see how you got this to compile, much less run all the way to a fault in the call to qsort. There may be more, but:

Code:
const Location *l = (Location*) left;
const Location *r = (Location*) right;

You are using "Location", but that is only typedef'd to struct _Location in main's local scope.

Code:
Location l[argv[2]]; //the size is given as a command

argv[2], assuming it exists, is a char *, and even if you cast it to a const int, it's not going to have the numerical value of the string you passed on the command line.

Code:
if (fscanf(GISfile, "%[^|]s", l[count].array[0]==1){

That line is definitely missing a ). My guess would be .array[0])==1){

Code:
qsort (l, size,sizeof(Location), compareLoc);

As far as I can see, there is no declaration size. If you meant to write above:

Code:
const int size = atoi(argv[2]);
Location l[size];

That would declare your second variable to qsort and set it to the number items in the array.

If you make those changes, you will be closer to a source file that at least compiles:
Code:
#include <stdio.h>
#include <stdlib.h>

struct _Location {
        char** array;
};

typedef struct _Location Location;


int compareLoc(const void *left, const void *right){

        const Location *l = (Location*) left;
        const Location *r = (Location*) right;

        if (l->array[0]<r-> array[0]) {
                return -1;
        } else if (l->array[0]==r-> array[0]) {
                return 0;
        } else{
                return 1;
        }
}

int main(int argc, char* argv[]){

        FILE* GISfile = fopen(argv[1], "r");

        int size = atoi(argv[2]);

        Location l[size]; //the size is given as a command

        int count =0;

        while (1){
                l[count].array = malloc(20*sizeof(char*));


                l[count].array[0] = malloc(121*sizeof(char)); //max data length of 121

                if (fscanf(GISfile, "%[^|]s", l[count].array[0])==1) {
                        for (int i=1; i<20; i++) {
                                l[count].array[i] = malloc(121*sizeof(char));
                        }
                } else {
                        break;
                }
        }

        Location* point = l;   // Did you mean to pass point to qsort?
        qsort (l, size,sizeof(Location), compareLoc);
}
 
terribly sorry, yes, you are right about passing the wrong thing to qsort. I was in a bit of rush to copy my code over because I wanted to only include the important parts and see if anything was fundamentally wrong. Here is the updated code (I've tested and it seems that assigning the value to array seems to be the problem...)
Code:
//use a struct to know id and offset
struct _Location{
 char** array; //point to array of strings
};
typedef struct _Location Location;

int compareLoc(const void *left, const void *right){ //compare two locations using their unique id's
	const Location* l = (Location*) left;
	const Location* r = (Location*) right;	
	if (l->array[0]<r->array[0]) {return -1;}
	else if (l->array[0]==r->array[0]) {return 0;}
	else {return 1;}
}
//recursive binary search that finds a string x
int binarySearch(Location temp[], int l, int r, char* x){
if (r>=1){
	int mid = 1+(r-l)/2;
	char* tmppointer = temp[mid].array[0]; // by default choose the ID
	if (strcmp(tmppointer, x)==0){	return mid;}
	if (strcmp(tmppointer, x)>0)
		return binarySearch(temp,l,mid-1,x);
	return binarySearch(temp,mid+1,r,x);
}
// element not found
return -1;
}
// Program Purpose: Store, Index and search for geographical records (int main is line 73)
int main(int argc, const char* argv[]){
FILE *ptr = fopen(argv[1], "r"); //given command file
if (ptr == NULL){printf("Input file %s not found\n", argv[1]); return 0;}
FILE *ptr2 = fopen(argv[2], "w");
if (ptr2 == NULL){printf("Output file %s not found\n", argv[2]); return 0;}
char command [256] = {'\0'}; //max command length 256
char gisfile [256] = {'\0'}; //the file to open
//two string arrays, first for reading commands, second for reading gis records
int size; //size of hash table

// each file begins with "db_file [file]" and "table_sz [size]"
while (strcmp(command,"db_file")!=0){fscanf(ptr, "%s",command); 
//printf("command was %s\n", command); //testing only
}
fscanf(ptr, "%s", gisfile); fscanf(ptr, "%s",command); 
fscanf(ptr, "%d", &size);
Location l [size];
Location* hashed = malloc(sizeof(Location)*size); //declare an array of pointers to location structs

int count = 0; // how many lines (for GISfile)
FILE* GISfile = fopen(gisfile, "r"); //get the GIS record file
	while(1){l[count].array = malloc(20*sizeof(char*));
	l[count].array[0] = malloc(121*sizeof(char));
        if(fscanf(GISfile, "%[^|]s", l[count].array[0])==1){ //ensure we can scan the first item
	for (int i=1; i<20; i++){
		l[count].array[i] = malloc(121*sizeof(char));
 		fscanf(GISfile, "%[^|]s", l[count].array[i]);
                printf("l[count].array[%d]: %s\n", i, l[count].array[i]); //the array elements are empty!
}// max length was 120 for an element
	count++; }
	else{break;}
fclose(GISfile);
// use quicksort on the array. Syntax: qsort(void* pointerToArray, size_t nelem, size_t width,int (*compare)(const void*, const void*)); [where the voids will be compared]
Location* point = l; qsort(point, count, sizeof(Location), compareLoc);