473,388 Members | 1,524 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

basic code from Eckel's c++ book

#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

Aug 21 '06 #1
2 1271

pauldepst...@att.net wrote:
#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();

Aug 21 '06 #2
pa**********@att.net wrote:
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.

Aug 21 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

40
by: post400 | last post by:
Hi, there is another famous book 'Writing solid code' but does it apply to Python ? Or it's usable only by Microsoft C programmers ? The author seems to be an ex-Microsoft guy ! Thanks ,...
1
by: Arindam Gupta | last post by:
I am searching for the softcopy of a book called "C++ Inside & Out" by Bruce Eckel. Can any one please help me find it? The book is out of print in the market. Thanks.
4
by: Pat | last post by:
Hi, Could you please suggest a good reference book for VB.NET? At present, I am working on VB.NET project. and looking for a book that would be useful as a reference book. Also, I would...
16
by: Jesse Liberty | last post by:
I am writing a new book on Visual Basic 2005, targeted at VB6 programmers, and to some degree VB.NET 1.x programmers. I'd like to sign up a (limited) number of volunteers to read the book and...
2
by: elnahrawi | last post by:
Download ebook http://books-download.com/?Book=1487-Visual+Basic+2005+Jumpstart Okay, all you VB6 developers--time's up. As of March 2005, Microsoft no longer supports this version of Visual...
13
by: Gary Wessle | last post by:
hi how can I use $wc -l file_name which puts out the number of lines. #include <cstdlib> using std::system; system (wc -l file_name) I get...
5
by: arnuld | last post by:
Hi, recently i downloaded "Thinking in C" CD from Bruce Eckel's web-site as i wanted to learn C++ from his book "Thinking in C++". the CD did not play well on my LIGNUX distro (well it's my...
2
by: phoover.eml | last post by:
Does anyone know how to get a copy of the "Annotated Solution Guide" for Bruce Eckel's "Thinking in C++" ebook? I have tried to order it from the mindview.net web page. However, I get an error...
97
by: Master Programmer | last post by:
An friend insider told me that VB is to be killled off within 18 months. I guess this makes sence now that C# is here. I believe it and am actualy surprised they ever even included it in VS 2003 in...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.