Search⌘ K
AI Features

Solution Review: An Array of Function Pointers

Explore how to store function addresses in an array of function pointers and call these functions sequentially in C. Understand the process through examples like doubling, tripling, and quadrupling integer values using referenced functions. This lesson helps you grasp efficient ways to manage and invoke multiple functions via pointers.

We'll cover the following...
...
C
# include <stdio.h>
void dbl ( int * ) ;
void tple ( int * ) ;
void qdpl ( int * ) ;
int main( )
{
int num = 2, i ;
void ( *p[ ] )( int * ) = { dbl, tple, qdpl } ;
for ( i = 0 ; i < 3 ; i++ )
{
p[ i ]( &num ) ;
printf ( "%d\n", num ) ;
}
return 0 ;
}
void dbl ( int *n )
{
*n = *n * *n ;
}
void tple ( int *n )
{
*n = *n * *n * *n ;
}
void qdpl ( int *n )
{
*n = *n * *n * *n * *n ;
}

Explanation

...