Exercise 3-34 (Array of pointers to functions returning strings)

Chapter_3     Exercise_3-33     FunctionTable Exercise_3-30     Exercise_3-35







Exercise 3-34     TCP1, p. 231


Exercise 3-34. Modify FunctionTable.cpp (see ch3-FunctionTable) so that each function returns a string (instead of printing out a message) and so that this value is printed inside of main().




CONTENTS:     function_table_string.c     FunctionTableString.cpp




function_table_string.c         download


// Using an array of pointers to functions
#include <stdio.h> // for printf(), getchar()
#include <string.h> // for strcpy(), strcat()

#define LENGTH 100 // max string length
// A macro to define dummy functions:
#define DF(N) char* N() \
{ \
char retval[LENGTH]; \
strcpy(retval, "Function "); \
strcat(retval, #N); \
strcat(retval, "() called..."); \
char* cp = retval; \
return cp; \
}

DF(a); DF(b); DF(c); DF(d); DF(e); DF(f); DF(g); // semicolons not required here

char* (*func_table[])() = {a, b, c, d, e, f, g};

int main()
{
char c;

while(1)
{
printf("Press a key from 'a' to 'g' or 'q' to quit\n");
c = getchar(); getchar(); // second one for newline (ENTER)

if (c == 'q') {break;} // out of while(1)
if (c < 'a' || c > 'g') {continue;} // function not defined
// else 'a' <= c <= 'g'
printf("%s\n", (*func_table[c - 'a'])());
printf("%s\n", func_table[c - 'a']()); // same function call
}

return 0;
}
/*
gcc function_table_string.c -o function_table_string
./function_table_string
Press a key from 'a' to 'g' or 'q' to quit
0
Press a key from 'a' to 'g' or 'q' to quit
h
Press a key from 'a' to 'g' or 'q' to quit
a
Function a() called...
Function a() called...
Press a key from 'a' to 'g' or 'q' to quit
g
Function g() called...
Function g() called...
Press a key from 'a' to 'g' or 'q' to quit
c
Function c() called...
Function c() called...
Press a key from 'a' to 'g' or 'q' to quit
q
*/











FunctionTableString.cpp         download


// Using an array of pointers to functions
#include <iostream>
#include <string>
using std::string;
using std::cout;
using std::cin;
using std::endl;

// A macro to define dummy functions:
#define DF(N) string N() \
{return "Function " + string(#N) + "() called...";}

DF(a); DF(b); DF(c); DF(d); DF(e); DF(f); DF(g); // semicolons not required here

string (*func_table[])() = {a, b, c, d, e, f, g};

int main()
{
char c, cr; // CR - carriage return or newline

while(1) // while(true)
{
cout << "Press a key from 'a' to 'g' or 'q' to quit" << endl;
cin.get(c); cin.get(cr); // second one for CR (ENTER)

if (c == 'q') {break;} // out of while(1)
if (c < 'a' || c > 'g') {continue;} // function not defined
// else 'a' <= c <= 'g'
cout << (*func_table[c - 'a'])() << endl;
cout << func_table[c - 'a']() << endl; // same function call
}

return 0;
}
/*
g++ FunctionTableString.cpp -o FunctionTableString
./FunctionTableString
Press a key from 'a' to 'g' or 'q' to quit
0
Press a key from 'a' to 'g' or 'q' to quit
h
Press a key from 'a' to 'g' or 'q' to quit
a
Function a() called...
Function a() called...
Press a key from 'a' to 'g' or 'q' to quit
g
Function g() called...
Function g() called...
Press a key from 'a' to 'g' or 'q' to quit
c
Function c() called...
Function c() called...
Press a key from 'a' to 'g' or 'q' to quit
q
*/









Chapter_3     Exercise_3-33     FunctionTable BACK_TO_TOP Exercise_3-30     Exercise_3-35



Comments

Popular posts from this blog

Contents