Function Pointers

Hello, I am writing a program that checks if a sort routine (in this case, insertion sort) being used is stable. For this particular program, I have to use function pointer to pass the function of the sort routine.

1. How do you pass insertion_sort function as a parameter? I've tried to do this but length and arr are not initialized so it doesn't know that I am taking insertion_sort and putting it into stable_sort

2. Why am I getting error messages in line 38 and 40?

Thank you for helping

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
  #include <iostream>
using namespace std;

bool stable_sort(void ) // how to pass insertion_sort function as a parameter?
{
	for (int i = 0; i < length; i++)
	{
		if (arr[i] < arr[i+1])
			return true;
		else
			cout << "The sort is not stable! " << endl;
			return false;
	}
}

void insertion_sort(int arr[], int length) 
{
	int i, j ,tmp;
	for (i = 1; i < length; i++) 
	{
		j = i;
		while (j > 0 && arr[j - 1] > arr[j]) 
		{
			tmp = arr[j];
			arr[j] = arr[j - 1];
			arr[j - 1] = tmp;
			j--;
		}//end of while loop
	}//end of for loop
}//end of insertion_sort.
int main()
{
	int c;
	int length = 5;
	int arr[5] = {5,4,3,2,1};
	void (*pfnc)(int arr[], int);
	pfnc = & insertion_sort;
	c = (*pfnc)(arr, length);

	stable_sort(c);

	return 0;
}
Why am I getting error messages in line 38 and 40?
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
bool stable_sort(int arr[], int length) // how to pass insertion_sort function as a parameter?
{
	for (int i = 0; i < length; i++)
	{
		if (arr[i] < arr[i+1])
			return true;
		else
			cout << "The sort is not stable! " << endl;
			return false;
	}
}

int main()
{
	int c;
	int length = 5;
	int arr[5] = {5,4,3,2,1};
	void (*pfnc)(int arr[], int);
	pfnc = insertion_sort;
	c = pfnc(arr, length);

	stable_sort(arr, length);

	return 0;
}
Last edited on
Thank you for helping, but I have few questions about your code.

1. If I implemented your code, what is the point of having code line 18 - 20?

2. I specifically mentioned that you are not supposed to pass array and int in bool stable_sort. Instead, you are supposed to pass a function pointer.
This function pointer is going to be the function of the sort (in this case, it is insertion sort). How can I pass the insertion_sort to the bool stable_sort?
Just like a normal function pointer. Normally I would say to use std::function, but considering this appears to be a homework assignment or the like I'll just show you the way its normally done:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
typedef void (*pfnSort)(int*, int);

bool stable_sort(pfnSort) {
    // ...
}

void insertion_sort(/* ... */) {
    // ...
}

int main() {
    pfnSort pfnc;
    pfnc = insertion_sort;
    // ...
    stable_sort(pfnc);
    return 0;
}


I'm not certain if I got this right though. Function pointers are just messy, its normally better to use std::function. This should give you an idea, though.

I'm not going to go into the logic errors in your code - I don't have time at the moment, I may reply back if I get the time. Hope you work it out, though!
If I implemented your code, what is the point of having code line 18 - 20?
It's your code. I just fixed the syntax.

I specifically mentioned that you are not supposed to pass array and int in bool stable_sort. Instead, you are supposed to pass a function pointer.
You can think of passing a pointer to a function as "passing code". You dynamically determine where that code is run.

But passing the function is just passing code. If you want to sort data, you need to coordinate the two.

It's not clear what you're looking for. Are you just looking for an example where this stuff is used?
Topic archived. No new replies allowed.