Let's go back to the sorting example where I suggested using a function pointer to write a generic sorting routine where the exact order could be specified by the programmer calling the sorting function. It turns out that the C function qsort does just that.From the Linux man pages, we have the following declaration for qsort (from stdlib.h): void qsort(void *base, size_t nmemb, size_t size, int(*compar)(const void *, const void *));Note the use of void*s to allow qsort to operate on any kind of data (in C++, you'd normally use templates for this task, but C++ also allows the use of void* pointers) because void* pointers can point to anything. Because we don't know the size of the individual elements in a void* array, we must give qsort the number of elements, nmemb, of the array to be sorted, base, in addition to the standard requirement of giving the length, size, of the input.But what we're really interested in is the compar argument to qsort: it's a function pointer that takes two void *s and returns an int. This allows anyone to specify how to sort the elements of the array base without having to write a specialized sorting algorithm. Note, also, that compar returns an int; the function pointed to should return -1 if the first argument is less than the second, 0 if they are equal, or 1 if the second is less than the first.For instance, to sort an array of numbers in ascending order, we could write code like this:#include <stdlib.h>int int_sorter( const void *first_arg, const void *second_arg ){ int first = *(int*)first_arg; int second = *(int*)second_arg; if ( first < second ) { return -1; } else if ( first == second ) { return 0; } else { return 1; }}int main(){ int array[10]; int i; /* fill array */ for ( i = 0; i < 10; ++i ) { array[ i ] = 10 - i; } qsort( array, 10 , sizeof( int ), int_sorter ); for ( i = 0; i < 10; ++i ) { printf ( "%d\n" ,array[ i ] ); }}
C++
Topic: Pointers
What are Function Pointers in the Wild ?
Browse random answers:
What is Pointers in c++ ?
What is importance of const. pointer in copy constructor?
Explain "passing by value", "passing by pointer" and "passing by reference"
Where we use reference and where we use pointers ?Write their differences and advantages and disadvantages .
Explain which of the following declarations will compile and what will be constant - a pointer or the value pointed at: * const char ** char const ** char * const
Why Pointers are not used in C Language. What are the main differences between C and C++
Explain about Reference operator (&) ?
Explain about declaring variables of pointer types with example ?
Explain about pointers and arrays ?
Explain about Pointer initialization ?
Explain about Pointer arithmetics ?
Explain about Pointers to pointers ?
Explain the void pointers with example ?
What is Null pointer ?
What is Pointers to functions ?
What Are Pointers?
What are Using Pointers in C++ ?
Explain Accessing the Value at the Memory Address held by a Pointer ?
Why Use Pointers?
What are C++ Pointer Syntax ?
Explain about Taking Stock of Pointers ?
What are pointers? Why should you care?
Explain about Manipulating Data with Pointers and example ?
What are Array of pointers ?
Where do we use pointers?
Why do we need pointers?
Allocating a memory from the "Free Store" ?
Explain about Allocating a memory with new operator ?
Explain about De-allocating a memory with delete operator ?
Explain about Data type class pointers with example ?
Explain about memory leaks associated with pointers ?
Explain about memory leak caused by pointer reassignment ?
When local variable’s name is same as member’s name
What are function Pointer Syntax ?
What are Reading Function Pointer Declarations ?
What are Using a Function Pointer ?
What are Function Pointers in the Wild ?
How to Using Polymorphism and Virtual Functions Instead of Function Pointers (C++) ?
What are Benefits of Function Pointers ?
Where They get Useful Function Arguments ?
Explain Smart Pointers (Modern C++) ?
What are Uses for smart pointers ?
What are Kinds of Smart Pointers ?
What are another Use of Pointers Efficient Array Processing ?
Explain about C++ Pointers and new
What are problems with pointers ?
Explain about generic smart pointer class ?
How to initialize a vector of pointers?
Explain about c++ Pointer Arithmetic ?
Explain about incrementing a Pointer ?
Explain decrementing a Pointer ?
Explain pointer Comparisons ?