0

For learning purpose, I want to create a pointer to the function void qsort (void* base, size_t num, size_t size, int (*compar)(const void*,const void*)) defined in cstdlib and use that pointer to function as argument for void* bsearch(const void* key, const void* base, size_t num, size_t size, int (*compar)(const void*,const void*)); defined in same library. My attempted code is this:

#include <iostream>
#include <cstdlib>
using namespace std;

class Student
{
public:
    Student(const char *_name, int _id) : name(_name), id(_id) {}
    int operator-(const Student &s) { return (id - s.id); }
private:
    const char *name;
    int id;
};

int compare_by_ID(const void *s1, const void *s2)
{
    return ( *(Student*)s1 - *(Student*)s2 ); 
}

typedef int (*compareFuncPtr)(const void *, const void *);
typedef void (*voidPtr)(void *, size_t, size_t, compareFuncPtr); /*how to use
                                                                 this pointer? 
                                                                 is it defined 
                                                                 correctly for 
                                                                 my purpose?*/
int main()
{
    Student s1("Bob", 8891);
    Student s2("Jack", 8845);
    Student s3("John", 8723);

    Student stdnts[] = {s1, s2, s3};

    compareFuncPtr compare;
    compare = compare_by_studentID;
    qsort(stdnts, 3, size0f(stdnts[0]), compare);    
    //voidPtr qsortPtr;
    //qsortPtr = ??? //to replace qsort(..) bellow:
    bsearch(&s3, qsort(stdnts, 3, sizeof(stdnts[0]), compare)); /*compile error:
                                                                 invalid use of
                                                             void expression.*/
}

How to replace arguments of bsearch(const void* key, const void* base, size_t num, size_t size,int (*compar)(const void*,const void*)) with a pointer to function? to use it like this: bsearch(key, pointerToFunction)

mhm
  • 313
  • 1
  • 5
  • 12
  • 1
    But what is your question here? – Abhijit Pritam Dutta Mar 05 '18 at 10:32
  • 2
    `qsort` has a `void` return type. You don't compose it with `bsearch`, if I understand what you are even trying to accomplish. – StoryTeller - Unslander Monica Mar 05 '18 at 10:34
  • I want to use `bsearch` like this `bsearch(key, functionPointer)` instead of passing all arguments. (for learning purpose to see if creating such pointer to function is possible) – mhm Mar 05 '18 at 10:52
  • 1
    If you are learning, don't make up constructs. You can't call a function in C++ with less arguments that what it specifies. *There is support* for functional style programming in C++, but this isn't it. If you really want to learn, then learn it well, in a structured manner, from a [good source of learning](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – StoryTeller - Unslander Monica Mar 05 '18 at 11:03
  • 1
    After `qsort(stdnts, 3, sizeof(stdnts[0]), compare);`, `bsearch(&s3, stdnts, 3, sizeof stdnts[0], compare);` would work. Actually, signatures of `qsort()` and `bsearch()` are very similar. The call of `qsort()` as 2nd arg. of `bsearch()` doesn't make any sense for the compiler. (Hence the error.) Try to become clear about _function pointer_ versus _function call_. (You cannot store a pointer to a function call which is automagically "unpacked" by another function call to provide arguments to it - C++ does not work this way.) – Scheff's Cat Mar 05 '18 at 12:35
  • So what do you expect `functionPointer` to do? Just play a role of a pack of arguments? – xskxzr Mar 05 '18 at 13:10

0 Answers0