How to write a C Program to simple demonstration of using pointers to determine which function to use in C Programming Language ?
Solution:
/*
* A simple demonstration of using pointers
* to determine which function to use
*
*/
#include <stdio.h>
void printAfterCalc(int input, const char *desc, int (*func)(int input));
int shiftLeft(int input);
int shiftRight(int input);
int addSeven(int input);
int integerDivideByTwo(int input);
void main(void) {
int val = 77;
printAfterCalc(val, "Default", NULL);
printAfterCalc(val, "Bitwise shift left", shiftLeft);
printAfterCalc(val, "Bitwise shift right", shiftRight);
printAfterCalc(val, "Add seven", addSeven);
printAfterCalc(val, "Divide by 2 (integer division)", integerDivideByTwo);
getchar();
}
void printAfterCalc(int input, const char *desc, int (*func)(int input)) {
int result;
if (func)
result = func(input);
else
result = input;
printf("%s: %d\n", (desc && *desc ? desc : "No description specified"), result);
}
int shiftLeft(int input) {
return (input << 1);
}
int shiftRight(int input) {
return (input >> 1);
}
int addSeven(int input) {
return (input + 7);
}
int integerDivideByTwo(int input) {
return (input / 2);
}
Learn More :
Pointer
- C Program Pointer Example
- C Program To Reverse The String Using Pointer
- C Program to Find max and min in array using pointer concept
- C Program to Adds literal to list of literals
- C Program to find smallest in an array using pointer
- C Program LEXICAL ANALYSER
- C Program Simple linked list
- File Handling (console to file) in C Program
Function
- How to pass one dimensional array to function in c.
- Write a c program which passes two dimension array to function.
- Write overloaded function templates for finding the roots of the linear (a * x + b = 0) and square (a * x2 + b * x + c = 0) uravneniy.Zamechanie: in function to send coefficients of the equations.
- C Program Character toupper() Example
- C Program Function Example
- Napisać funkcję obliczającą funkcję geometryczną w tablicy NxM elementowej z elementów o wartościach parzystych znajdujących się pod główną i ponad przeciwną przekątną.
- C Program To Find LCM and HCF Of Two Number Using Function - 2
- C Program To Convert Temperature In Celsius To Fahrenheit, Using Function
- C Program To Find Simple Interest
- C Function to Check Vowel
- Factorial Program In C Using Function
- C Program For Prime Number Using Function
- C Function to xorSwap and addSwap in C Programming
- C Program to concatenate two strings without using string functions
- C Function to read instructions from the file and obey them
- C program calculates a given function in range given by user, stores the data in arrays and displays the answer in a table.
- C Program to Implements a dictionary's functionality.
- = (int*)malloc(sizeof(int)) ??
- Design a C function that shortens a string to 8 characters
- C Program to Implements a dictionary's functionality
- C Program that prompts the user to input a string, (whitespaces allowed)
- Local sounds functions in C Program
- Function Get the resource that will be gathered from the zone name
- C Program to Find the Size of File using File Handling Function
- Average function in C