home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C Programming Starter Kit 2.0
/
SamsPublishing-CProgrammingStarterKit-v2.0-Win31.iso
/
tyc
/
list15_9.c
< prev
next >
Wrap
C/C++ Source or Header
|
1993-10-16
|
849b
|
58 lines
/* Using a pointer to call different functions. */
#include <stdio.h>
/* The function prototypes. */
void func1(int x);
void one(void);
void two(void);
void other(void);
main()
{
int a;
for (;;)
{
puts("\nEnter an integer between 1 and 10, 0 to exit: ");
scanf("%d", &a);
if (a == 0)
break;
func1(a);
}
}
void func1(int x)
{
/* The pointer to function. */
void (*ptr)(void);
if (x == 1)
ptr = one;
else if (x == 2)
ptr = two;
else
ptr = other;
ptr();
}
void one(void)
{
puts(" You entered 1.");
}
void two(void)
{
puts("You entered 2.");
}
void other(void)
{
puts("You entered something other than 1 or 2.");
}