Click here to Skip to main content
12,452,083 members (59,030 online)
Click here to Skip to main content

Stats

29.1K views
2.7K downloads
53 bookmarked
Posted

Secure Function Pointer and Callbacks in Windows Programming

, 4 May 2011 CPOL
This article explains the usage of function pointer and callback in Windows application programming Interface (API).
/////////////////////////////////////////////////////////////////////////////////////////////////////////
//
//
// CFunctionPointer.cpp : Defines the entry point for the console application.
//
//
//
/////////////////////////////////////////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include<stdio.h>

/****************************************************************
BubbleSort	 - Sort the array of integer using Bubble Sort
Arguments	 - Array of integer
			 - Number of array elements
Retrun		 - Sorted array using function pointer.
*******************************************************************/
void BubbleSort(int arr[], int n) {
      bool swapped = true;
      int j = 0;
      int tmp;
      while (swapped) {
            swapped = false;
            j++;
            for (int i = 0; i < n - j; i++) {
                 if (arr[i] > arr[i + 1]) {
                        tmp = arr[i];
                        arr[i] = arr[i + 1];
                        arr[i + 1] = tmp;
                        swapped = true;
                 }
            }
      }
}

/****************************************************************
Display		 -	Display the array of integer
Arguments	 - Array of integer
			 - Number of array elements
Retrun		 - void. Display in Console
*******************************************************************/
void Display(int arr[], int n) {
	for(int i=0;i<n;i++)
		printf("%d ",arr[i]);
	printf("\n");

}


/****************************************************************
BinarySearch		 -	Recursive search the elements using BinarySearch algorithm callback function
Arguments			- Array of sorted integer
					- Search element
					- Array min value
					- Array Max index value
Retrun				- -1 if not match
					- return N th match element position
*******************************************************************/
int BinarySearch(int arr[], int value, int left, int right) {
	printf("\nSearch using BinarySearch...\n");
      while (left <= right) {
            int middle = (left + right) / 2;
            if (arr[middle] == value)
                 return middle;
            else if (arr[middle] > value)
                  right = middle - 1;
            else
                 left = middle + 1;

      }
      return -1;
}


/****************************************************************
BinarySearch		 -	Linear search the elements using LinearSearch algorithm callback function
Arguments			- Array of integers
					- Search element
					- Array min value
					- Array Max index value
Retrun				- -1 if not match
					- return N th match element position
*******************************************************************/
int LinearSearch(int arr[], int value, int left, int right) {
	printf("\nSearch using LinearSearch...\n");
	for(int i=left;i<right;i++)
	{
		if(arr[i]==value)
			return i;
	}
    return -1;
}

/****************************************************************
 The calling function takes a single callback as a parameter. 
*******************************************************************/
void Search(int (*SelectSearch)(int arr[], int, int,int),int arr[],int value,int left,int right) {

	int result = SelectSearch(arr,value,left,right);
	if(result == -1)
		printf("Search element not found!\n");
	else
		printf("Element %d found in %d position!\n", value,result);

}

// Declare function pointer
typedef void (*FunctionPointer)(int arr[], int);

int _tmain(int argc, _TCHAR* argv[])
{
	// Declare and define the variable
	int arr[]={1,9,8,4,6,5,3,7,2};
	int searchval = 6;
	int result = 0;
	FunctionPointer fpsort = NULL;
	FunctionPointer fpdisplay = NULL;
	
	printf("Display Array Before sort using function pointer: ");
	fpdisplay = &Display;
	if(fpdisplay)
		(*fpdisplay)(arr,9);

	//Call Search with LinearSearch callbacks
	Search( LinearSearch,arr,searchval,0,9);

	fpsort = &BubbleSort;

	printf("\nSort Array using Bublesort using function pointer: ");
	if(fpsort)
		(*fpsort)(arr,9);

	if(fpdisplay)
		(*fpdisplay)(arr,9);

	//Call Search with LinearSearch and  BinarySearch callbacks
	Search( LinearSearch,arr,searchval,0,9);
	Search( BinarySearch,arr,searchval,0,9);

	
	return 0;
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

R.selvam
Software Developer (Senior)
India India
Selvam has worked on several technologies like Core Java, VC++, MFC, Windows API and Weblogic server. He takes a lot of interest in reading technical articles and enjoy writing them too. He has been awarded as a Microsoft Community Star in 2004, MVP in 2005-06, SCJP 5.0 in 2009, Microsoft Community Contributor(MCC) 2011.

Github : https://github.com/selvamselvam
Web site: http://www.careerdrill.com

You may also be interested in...

Pro
Pro
| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160826.1 | Last Updated 4 May 2011
Article Copyright 2011 by R.selvam
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid