basic code from Eckel's c++ book

P

pauldepstein

#include <iostream>
using namespace std;

// A macro to define dummy functions:
#define DF(N) void N() { \
cout << "function " #N " called..." << endl; }

DF(a); DF(b); DF(c); DF(d); DF(e); DF(f); DF(g);

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

int main() {
while(1) {
cout << "press a key from 'a' to 'g' "
"or q to quit" << endl;
char c, cr;
cin.get(c); cin.get(cr); // second one for CR
if ( c == 'q' )
break; // ... out of while(1)
if ( c < 'a' || c > 'g' )
continue;
(*func_table[c - 'a'])();
system("PAUSE");
}
} ///:~

This code has been copy-pasted from Eckel's Thinking in c++. I added
the system line so that I could test it on my Windows XP (Probably
Eckel would have preferred a different change since system ("PAUSE");
is not portable).

It does work, and I almost understand it, but not quite.

This is how I see it: {a, b, c, d, e, f, g} is an array of pointers
to functions.

This array is called func_table. So a is a pointer which = =
func_table[0], b is a pointer which == func_table[1] etc.

However, this is clearly wrong because a and b are actually functions,
not pointers at all.

My problem is that, on one hand, we seem to have an array of pointers
to functions.

Yet, on the other hand, these "pointers to functions" are called a, b,
c, d, e, f, g.

If they are pointers, shouldn't they have names which correspond to
addresses?

Perhaps, someone could clarify the key points.

[I _do_ understand how the macro concept is being applied. I
understand fully how the macro creates seven functions: void a(), void
b() etc., so no need to explain that part.]

Thank you very much,

Paul Epstein
 
A

amparikh

#include <iostream>
using namespace std;

// A macro to define dummy functions:
#define DF(N) void N() { \
cout << "function " #N " called..." << endl; }

DF(a); DF(b); DF(c); DF(d); DF(e); DF(f); DF(g);

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

int main() {
while(1) {
cout << "press a key from 'a' to 'g' "
"or q to quit" << endl;
char c, cr;
cin.get(c); cin.get(cr); // second one for CR
if ( c == 'q' )
break; // ... out of while(1)
if ( c < 'a' || c > 'g' )
continue;
(*func_table[c - 'a'])();
system("PAUSE");
}
} ///:~

This code has been copy-pasted from Eckel's Thinking in c++. I added
the system line so that I could test it on my Windows XP (Probably
Eckel would have preferred a different change since system ("PAUSE");
is not portable).

It does work, and I almost understand it, but not quite.

This is how I see it: {a, b, c, d, e, f, g} is an array of pointers
to functions.

This array is called func_table. So a is a pointer which = =
func_table[0], b is a pointer which == func_table[1] etc.

However, this is clearly wrong because a and b are actually functions,
not pointers at all.

My problem is that, on one hand, we seem to have an array of pointers
to functions.

Yet, on the other hand, these "pointers to functions" are called a, b,
c, d, e, f, g.

If they are pointers, shouldn't they have names which correspond to
addresses?

Perhaps, someone could clarify the key points.

[I _do_ understand how the macro concept is being applied. I
understand fully how the macro creates seven functions: void a(), void
b() etc., so no need to explain that part.]

Thank you very much,

Paul Epstein

Jusr as an arry decays to a pointer, a function by default also decays
to a pointer.
Therefore, whether you have the function ptr array initialized with

a, b ...

or

&a, &b...

It means the same.

therefore if you have a func ptr,
you can therefore the function either as

(*fp)(); or fp();
 
R

Ron Natalie

However, this is clearly wrong because a and b are actually functions,
not pointers at all.

You are correct, they are not pointers. The names refer to functions.

Names of non-member functions automatically get converted to a pointer
to the function. It's an old C artifact (similar to the
array-to-pointer conversion).

You could write &a, &b, ... and it would end up being the smae.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top