Re: K&R2 Secition 5.9 - major blunders
From: Kiru Sengal (kiru.sengal_at_gmail.com)
Date: 03/07/05
- Next message: Warren Postma: "Re: if (-1) returns true"
- Previous message: Michael Mair: "Re: printf("%p\n", (void *)0);"
- In reply to: TTroy: "K&R2 Secition 5.9 - major blunders"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 7 Mar 2005 08:12:22 -0800
TTroy wrote:
> I FOUND MAJOR ERRORS in K&R2 (making it almost useless for the herein
> mentioned topics).
>
> K&R2 Section 5.9 Pointers vs. Multidimension Arrays starts of like
> this...
>
> "Newcomers to C are somtimes confused about the difference between a
> two-dimensional array and an array of pointers..."
>
Probably.
>
> then continues to explain int *b[10]; to be...
>
> "For b, however, the definition only allocates 10 pointers ...
Assuming
> that each element of b does point to a twenty element array,then
there
> will be 200 ints set aside, plus ten cells for pointers... "
>
Would changing 'point to a' to 'point into a' twenty element array be
better for you? If so you are free to do so in your copy of the book.
>
> Is it just me, or is that explanation only valid for int
(*b[10])[20];
> ????
>
Well, it does and it doesn't. It fits the part of the explanation that
says "Assuming each element of b does point to a twenty element array"
as you have quoted, but it doesn't fit the part that comes after what
you have quoted that explains how each array/row can be of different
lengths. The whole section read in its entirety does lead one to
realize that 'to' really means 'into.' The reader should be able to
figure that out.
>
> I'm pretty sure K&R2 is totally wrong in this section (had me
confused
> for a while).
>
> Later on in section 5.9, it says "... by far the most frequent use of
> arrays of pointers is to store character strings of diverse
lengths..."
>
Take a pen, and change 'store' to 'manage.' K&R didn't get too
detailed here probably because the reader would already know what's
going from reading the well explained in previous sections on the issue
(5.5, 5.6). The diagram at the end of 5.9 also serves as an good
comparison between what was really happening (arrays of pointers to
strings stored elsewhere) and what it might be confused with (arrays of
strings held internally - 2 dimensional char array).
Incidentally, section 5.10 also explains *argv[] as "a pointer to an
array of character strings" and says "argv[0] is the name by which the
program was invoked." So go ahead and change those to "a pointer to a
pointer to a string(this probably would confuse beginners)" and
"argv[0] points to the program name."
The changes aren't really needed, since the diagram clearly shows what
is meant, and a beginner might not be able to get passed the section if
it was 110% accurate. Learning is cumulative, and it's sometimes better
to refrain from being too pedantic early on in someone's education of a
subject, because being so might mentally "hold them back" from learning
further. In your early years of learning chemistry, you were probably
taught basic notions of K, L, M, N shells, but in your later years you
probably thought "they were totally wrong a few years ago!!." No they
weren't, they refrained from being 100% detailed/accurate to help you
move on and learn other aspects of the earlier course (which were also
probably 'less than 100% accurate/detailed').
>
> Once again, this seems totally wrong.
> char *strings[] = {"Bob","Dave","Jack");
> 'strings' is only managing pointers to anonymous objects that are
> automatically created by the initialization (which there is no
mention
> of in this section). 'strings' doesn't strore "Bob" "Dave" "Jack".
>
See now, you are making the same type of statement K&R were making
about the pointers to int arrays. See where you said "'strings' is only
managing pointers 'to' anonymous objects' instead of "'strings' is only
managing pointers 'to the first element..'" So from context it's easy
to tell what is meant by 'pointing to' an object or array.
Technically, pointing to an object/array means the pointer has a
reference type with size equal to the whole objects, but there is no
harm with using 'pointing to' to describe 'pointing into the first
element of' because context clears up the confusion.
Probably the most unambiguous use of 'pointing to' to refer to
'pointing into first element of' is with the word "strings." Almost
nobody says things like (char *b = "bill";) b is a pointer to the
'first element of a string.' Almost everyone says b is a pointer to a
string. This is similar to the 'to an object' vs 'into first element
of an object' I mentioned before, but has no ambiguity because everyone
knows strings are handled by pointers to their first characters (IOW,
"strings" is enough context to clear things up).
>
>
[snipped an opinion of yours]
>
>
Take care
__________________________________
~A Hardware Guy Programming in C~
kiru dot sengal at gmail dot com
__________________________________
- Next message: Warren Postma: "Re: if (-1) returns true"
- Previous message: Michael Mair: "Re: printf("%p\n", (void *)0);"
- In reply to: TTroy: "K&R2 Secition 5.9 - major blunders"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|