Implement a Text-Based Menu System using a pointer-to-function. - C Pointer

C examples for Pointer:Function Pointer

Description

Implement a Text-Based Menu System using a pointer-to-function.

Demo Code

#include <stdio.h>

void cut(int intCut);
void copy(int intCopy);
void paste(int intPaste);
void delete1 (int intDelete);

int main()//from ww w .j  av a2 s. c  o m
{
   void(*funcPtr[4])(int) = { cut, copy, paste, delete1 };
   int intChoice;
   printf("Your choices (0, 1, 2, or 3).\n");
   printf("Enter 0 to Cut.\n");
   printf("Enter 1 to Copy.\n");
   printf("Enter 2 to Paste.\n");
   printf("Enter 3 to Delete.\n");
   scanf("%d", &intChoice);
   (*funcPtr[intChoice])(intChoice);
   return(0);
}

void cut(int intCut)
{
   printf("You entered %d.\n", intCut);
   printf("Cut.\n");
}

void copy(int intCopy)
{
   printf("You entered %d.\n", intCopy);
   printf("Copy.\n");
}

void paste(int intPaste)
{
   printf("You entered %d.\n", intPaste);
   printf("Paste.\n");
}

void delete1 (int intDelete)
{
   printf("You entered %d.\n", intDelete);
   printf("Delete.\n");
}

Result


Related Tutorials