qsort parameter function not built properly.  Using Structures.

Posted on 2003-03-17
Medium Priority
Last Modified: 2007-12-19
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.

Thank you in advance,
Question by:cpalacios
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2

Accepted Solution

Nosfedra earned 600 total points
ID: 8155603
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.


Assisted Solution

Kocil earned 400 total points
ID: 8164346

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

Expert Comment

ID: 8164352
Ooops, a correction

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


Expert Comment

ID: 9502321
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:

Please Review

Please leave any comments here within the next seven days.


EE Cleanup Volunteer

Featured Post

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses
Course of the Month8 days, 22 hours left to enroll

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question