void pointers


Hey guys believe it or not I am now only coming to grips with function pointers,anyway I wrote a simple program that uses the sqort function that obviously sorts an array,everything works fine the way I expected it but I have one question,

when we call the qsort function,we call it with compare,but how come we don't have to give compare the two arguments it takes?

1
2
3
4
5
6

    qsort(ray,10,sizeof(int),compare(1,6); // wrong
    qsort(ray,10,sizeof(int),compare); // correct,but how come?





thanks

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48

#include <iostream>
#include <stdlib.h>

using namespace std;


void printNum(int a,int b){

  cout << a + b << endl;
}

int compare(const void *a,const void *b){

   int x = *(int*)a;
   int y = *(int*) b;

   if(x == y){

      return 0;
   }
   else if(x < y){

     return -1;
   }else{

     return 1;
   }
}

int main()
{
    void (*print)(int,int);
    print = &printNum;
    print(5,2);
    int ray[10] = {1,3,6,8,2,3,4,83,44,2};

    qsort(ray,10,sizeof(int),compare);

    for(int i = 0; i < 10; i++){


        cout << ray[i] << endl;
    }  // print contents of array

    return 0;
}
1
2
    qsort(ray,10,sizeof(int),compare(1,6); // wrong
    qsort(ray,10,sizeof(int),compare); // correct,but how come? 

compare(1,6) is attempting to call the function itself, and then return an int. [Think about it; what would the 1 and 6 even mean? qsort is going to be calling compare at least 10 times (on average, 10*log(10) times).]

Passing compare itself (the name of the function) is the same thing as passing a pointer to that function. Inside qsort is where the compare function pointer is dereferenced to actually call the function.

Is there a particular reason you're using qsort? C is type-unsafe in that many library functions convert their input to void*, the lowest common denominator of all data, in order to provide re-usability. C++ has its own version of sort, std::sort, which is templated and therefore typesafe.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// Example program
#include <iostream>
#include <algorithm> // std::sort

int main()
{
    int arr[5] = {4, 1, 5, 3, 2};
    
    std::sort(arr, arr + 5);
    
    for (int i = 0; i < 5; i++)
    {
        std::cout << arr[i] << " ";   
    }
}
Last edited on
Hey Ganado,good point and not really just learning to get a better understanding of what is going on at a lower level

Passing compare itself (the name of the function) is the same thing as passing a pointer to that function. Inside qsort is where the compare function pointer is dereferenced to actually call the function.


so in the qsort function pointer something like this will happen?
compare = compare(ray[0],ray[1])

and so on until it reaches the last piece of data


but yeah it would be a smarter idea to use std::sort
so in the qsort function pointer something like this will happen?
compare = compare(ray[0],ray[1])

Yes, except it wouldn't be assigning the result to compare, that doesn't quite make sense. It would be using the result of compare(ray[i], ray[j]) in an if-statement to see if or where it needs to move/swap elements in the array (qsort uses the quicksort algorithm to sort, hence the name).
Last edited on
thanks Ganado that makes sense :)
Topic archived. No new replies allowed.