Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
• Status: Solved
• Priority: Medium
• Security: Public
• Views: 323

# qsort parameter function not built properly. Using Structures.

Hi there,

I am trying to build a program that uses the qsort function.  The qsort function looks like this:

qsort( (void *) rotu, numrecs, sizeof(struct ROTU), compare_distances);

The function it needs as the last parameter is compare_distances and it's below:'

int compare_distances( struct ROTU_LOOKUP *A, struct ROTU_LOOKUP *B)
{
float distA, distB; // distance metric from cotu to A vs distance metric to B
// actual distance is sqrt(  ((cotu.x-A.x)**2) + ((cotu.y-A.y)**2)  ) ,
// but we don't need the square root when just comparing for sorting

distA = (cotu.latitude - rotl[A]->latitude) * (cotu.latitude - rotl[A]->latitude)+
(cotu.lngitude - rotl[A]->lngitude) * (cotu.lngitude - rotl[A]->lngitude);

distB = (cotu.latitude - rotl[B]->latitude) * (cotu.latitude - rotl[B]->latitude)+
(cotu.lngitude - rotl[B]->lngitude) * (cotu.lngitude - rotl[B]->lngitude);

// qsort needs to know:
if( distA > distB) return( 1);  // distance A to cotu GT distance B to cotu
if( distA < distB) return(-1);  // distance A to cotu LT distance B to cotu
return( 0);  // distances A & B to cotu equal
}

I keep getting and error at this line:
distA = (cotu.latitude - rotl[A]->latitude) * (cotu.latitude - rotl[A]->latitude)+
(cotu.lngitude - rotl[A]->lngitude) * (cotu.lngitude - rotl[A]->lngitude);
- it gives me this error:
error C2107: illegal index, indirection not allowed

I am trying to use structures and this is how the structure the function above is using looks:
struct ROTU_LOOKUP {  // 1-to-1 correspondance of recno's to lat/longs
float latitude;
float lngitude;
} *rotl;              // the table of all records lat/longs in memory

What does the error I'm getting mean?  Am I calling it wrong?  Not too sure as I'm not great with structures.

Chris
0
cpalacios
• 2
2 Solutions

Commented:
Well... You don't use the functions' parameters right.

>>qsort( (void *) rotu, numrecs, sizeof(struct ROTU), compare_distances);

passing the rotu array of structures.

You can't use the names of the variables you called the qsort as they would _probably_ run out of scope.

To sort out the problem, replace rotl[A]->... with A-> since A is the name of the variable the function gets.

I really don't know what cotu stands for, but it should be a global scope variable.

Aplying these changes should probably start your program, but you __really__ should try some basic approaches to C and C++ function parameters calling, pointers and structures.

Regards,
--Razvan
0

Commented:

I have rearanged it to make it clear;

// the structure
struct ROTU_LOOKUP {
float latitude;
float lngitude;
};

// global variable
struct ROTU_LOOKUP cotu;

int compare_distances(struct ROTU_LOOKUP *A, struct ROTU_LOOKUP *B)
{
double distA, distB;
distA = (cotu.latitude - A->latitude) * (cotu.latitude - A->latitude)+
(cotu.lngitude - A->lngitude) * (cotu.lngitude - A->lngitude);

distB = (cotu.latitude - B->latitude) * (cotu.latitude - B->latitude)+
(cotu.lngitude - B->lngitude) * (cotu.lngitude - B->lngitude);

// qsort needs to know:
if( distA > distB) return( 1);
if( distA < distB) return(-1);
return( 0);
}

// the array of ROTU_LOOKUP
struct ROTU_LOOKUP *rotl;
int numrecs;

// now how we call it
main()
{
// initialize cotu
cotu.latitude = 0;
cotu.lngitude = 0;

// alloc some memory for array
rotl = malloc(1000 * sizeof(struct ROTU_LOOKUP));
numrecs = 0;

// add some rotus to rotl and increase numrecs
...

// then sort it
qsort(rotl, numrecs, sizeof(struct ROTU_LOOKUP*), compareRotu);

...
}

Just to let you know that comparing floating point may have problem.
http://www.experts-exchange.com/Programming/Programming_Languages/C/Q_20548837.html
0

Commented:
Ooops, a correction

// qsort(rotl, numrecs, sizeof(struct ROTU_LOOKUP*), compareRotu);
qsort(rotl, numrecs, sizeof(struct ROTU_LOOKUP*), compare_distances);

0

Commented:
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is: