 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Rick Rajter Guest
|
Posted: Wed Jul 14, 2004 9:10 pm Post subject: Practical C++ programming. Array of pointers |
|
|
Hi everyone. Minimal background info, then my problem/question
I am trying to relearn/increase my knowledge of C++ for some upcoming
projects that I will need to do for grad school. I read through the
oreilly "practical c++ programming book". Decent, though they tend to
spend a lot of time with the theory of coding (which is not to say
that this information isn't important). Anyway, the one part of the
book that really stumped me was the array of pointers. The example
just confused the heck out of me, and upon coding up a quick and dirty
c++ file, i'm tending to believe that it's incorrect (or at least
specific to structs)...
Here's the code.
// Pointer to the data
struct mailing *list_ptrs[MAX_ENTRIES];
int current; // Current mailing list entry
// ....
for (current = 0; current = number_of_entries; ++current) {
list_ptrs = &list[current];
++list_ptrs;
}
// Sort list_ptrs by zip code
basically, this is just a skeleton saying that we want to get an array
of pointers to all of the structs from the array of structs called
list
My question.
the line "list_ptrs = &list[current]", should their not be a similar
index on list_ptrs? I am under the impression (perhaps wrongly so)
that list_ptr is the address of the 0 position of the list_ptr array.
When in fact, we want to assign the "list" address of index "current"
to the pointer in array "list_ptr" of index "current". And the
incrementer at the end definitely stumps me. Why would you want to
move the 0 position of the array? wouldn't it then be impossible (or
at least annoying to do a *(list_ptr-index)) to access the pointer
data that one just stored?
I wrote a quick and dirty script (granted, using int arrays instead of
struct arrays)
#include<iostream>
int main()
{
const int COUNT=6;
int i=0;
int array[COUNT] = {1,4,6,8,9,13};
int *array_ptr[COUNT];
for(i=0;i
{
array_ptr[i]=&array[i];
}
std::cout<<" "<
}
And I was able to get 13 13 for output, which is the way I expected
it.
Trying to compile how the book states, I get compile errors.
So final question, is the book correct but just in a different way?
Or is their syntax old, or for classes/structs. I've been running
this through my head for a while, and can't seem to feel satisfied
that the first way listed could be a good way to do it.
Thanks in advance to any help
-rick
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
David Fisher Guest
|
Posted: Thu Jul 15, 2004 10:28 am Post subject: Re: Practical C++ programming. Array of pointers |
|
|
"Rick Rajter" asked:
| Quote: | ... the one part of the book that really stumped me was
the array of pointers. The example just confused the heck
out of me, and upon coding up a quick and dirty c++ file,
i'm tending to believe that it's incorrect (or at least specific
to structs)...
Here's the code.
// Pointer to the data
struct mailing *list_ptrs[MAX_ENTRIES];
int current; // Current mailing list entry
// ....
for (current = 0; current = number_of_entries; ++current) {
list_ptrs = &list[current];
++list_ptrs;
}
// Sort list_ptrs by zip code
|
Incrementing an array name (list_ptrs) is definitely a problem ... if you
have int x[20] you can't say "x++". The error message from GCC is
"non-lvalue in increment".
Try replacing the body of the loop with:
list_ptrs[current] = &list[current];
I also assume it should say "current != number_of_entries", not "current =
number of entries". It would be even better to use "<" just in case
number_of_entries has a negative value (anything is possible .
If the original code had somehow worked, list_ptrs would have ended up
pointing beyond the end of the array anyway ...
One other small thing: in C++, declaring a "struct X" implicitly defines a
type called "X", so saying "struct mailing ..." is redundant (just
use"mailing").
David Fisher
Sydney, Australia
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Paul Guest
|
Posted: Thu Jul 15, 2004 10:37 am Post subject: Re: Practical C++ programming. Array of pointers |
|
|
Could we see some pertinent compile errors?
Also the syntax:
struct struct_name var_name;
for initialization is old C syntax, I believe- I may be wrong here.
Certainly its an uncommon style for modern C++.
-paul
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Francis Glassborow Guest
|
Posted: Thu Jul 15, 2004 11:49 pm Post subject: Re: Practical C++ programming. Array of pointers |
|
|
In article <5QiJc.19159$%r.203691 (AT) nasal (DOT) pacific.net.au>, David Fisher
<david (AT) hsa (DOT) com.au> writes
| Quote: | Incrementing an array name (list_ptrs) is definitely a problem ... if you
have int x[20] you can't say "x++". The error message from GCC is
"non-lvalue in increment".
Try replacing the body of the loop with:
list_ptrs[current] = &list[current];
I also assume it should say "current != number_of_entries", not "current =
number of entries". It would be even better to use "<" just in case
number_of_entries has a negative value (anything is possible .
If the original code had somehow worked, list_ptrs would have ended up
pointing beyond the end of the array anyway ...
One other small thing: in C++, declaring a "struct X" implicitly defines a
type called "X", so saying "struct mailing ..." is redundant (just
use"mailing").
|
Unfortunately even the 2nd edition of that book is littered with such
poor code as well as poor design. These are not just typos, or even
thinkos but code that is either plain wrong or was poor a decade ago.
If you want to see a review of the book (not by me) try
http://www.accu.org/cgi-bin/accu/rvout.cgi?from=0ti_p&file=p003502a
and note that Paul is usually a much kinder reviewer than I am.
--
Francis Glassborow ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/youcandoit/projects
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Ray Lischner Guest
|
Posted: Fri Jul 16, 2004 12:20 am Post subject: Re: Practical C++ programming. Array of pointers |
|
|
On Wednesday 14 July 2004 05:10 pm, Rick Rajter wrote:
| Quote: | I read through the oreilly "practical c++ programming book".  ...
Anyway, the one part of the book that really stumped me was the array
of pointers.
|
The book might be wrong, or perhaps you mistyped the example. Which
example is it?
--
Ray Lischner, author of C++ in a Nutshell
http://www.tempest-sw.com/cpp
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
llewelly Guest
|
Posted: Fri Jul 16, 2004 11:20 am Post subject: Re: Practical C++ programming. Array of pointers |
|
|
[email]rickrajter (AT) mac (DOT) com[/email] (Rick Rajter) writes:
| Quote: | Hi everyone. Minimal background info, then my problem/question
I am trying to relearn/increase my knowledge of C++ for some upcoming
projects that I will need to do for grad school. I read through the
oreilly "practical c++ programming book".
|
Not a practical choice. I encountered this book while tutoring
some individuals who had relied on it. They all picked up many
misconceptions and bad habits from it. I think if O'Reilly knew
how bad it was they would stop printing it. (I have so many other
high-quality books from O'Reilly, that I find this one quite
unusual.) I suggest you throw it away. Normally the thought of
throwing a book away appalls me, but in this case I make an
exception.
I reccomend Koenig & Moo Accelerated C++, Lippman's Essential C++, or
Stroustrup's TC++PL3.
| Quote: | Decent, though they tend to
spend a lot of time with the theory of coding (which is not to say
that this information isn't important). Anyway, the one part of the
book that really stumped me was the array of pointers. The example
just confused the heck out of me, and upon coding up a quick and dirty
c++ file, i'm tending to believe that it's incorrect (or at least
specific to structs)...
Here's the code.
// Pointer to the data
struct mailing *list_ptrs[MAX_ENTRIES];
|
'struct' is unecessary in C++.
| Quote: | int current; // Current mailing list entry
// ....
for (current = 0; current = number_of_entries; ++current) {
list_ptrs = &list[current];
|
This assignment is a compile-time error.
So is this increment.
| Quote: | }
[snip]
So final question, is the book correct but just in a different way?
|
No. It is wrong. Unfortunately, from what I recall this example is
typical - or even better than average.
[snip]
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Francis Glassborow Guest
|
Posted: Fri Jul 16, 2004 1:55 pm Post subject: Re: Practical C++ programming. Array of pointers |
|
|
In article <861xjcjruu.fsf (AT) Zorthluthik (DOT) local.bar>, llewelly
<llewelly.at (AT) xmission (DOT) dot.com> writes
| Quote: | I reccomend Koenig & Moo Accelerated C++, Lippman's Essential C++, or
Stroustrup's TC++PL3.
|
Or even Lippman and Lajoie's C++ Primer.
BTW, do not just throw the book (Practical C++) away, take it back to
the supplier and ask for a refund (OK, they might not give it) on the
grounds that the book is not fit for the purpose for which you bought it
because of the level of incorrect information in it. At the very least
the supplier will then raise the issue with the publisher. BTW despite
my original review the book went to a second edition. I was invited to
technically review the book before publication and I agreed subject to
the caveat that my name might not be used unless I was satisfied that
the final text was substantially correct. At which point the publisher
(more correctly the specific editor) withdrew the invitation. Make your
own conclusion.
At the time he wrote his review of the second edition, AFAIK, Paul had
not read my original review nor was he aware of the invitation for me to
technically review it.
--
Francis Glassborow ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/youcandoit/projects
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Rick Rajter Guest
|
Posted: Sun Jul 18, 2004 11:02 am Post subject: Re: Practical C++ programming. Array of pointers |
|
|
I guess I'm a little dissappointed about this book then, as every
other O'reilly book I have is extremely helpful and fairly error free.
I'll take some of your book recommendations. Thanks for all your help
everyone.
-rick
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|