
Massacring C Pointers (2018) - pcr910303
https://wozniak.ca/blog/2018/06/25/1/index.html
======
susam
Fortunately, 18 years ago, I began learning C from the book The C Programming
Language, 2nd ed. written by Brian Kernighan and Dennis Ritchie.

The subject of pointers was generally believed to be scary among fellow
students and many of them bought pretty fat books that were dedicated solely
to the topic of pointers.

However, when I reached Chapter 5 of the The C Programming Language (K&R), I
found that it did a wonderful job at teaching pointers in merely 31 pages. The
chapter opens with this sentence:

> A pointer is a variable that contains the address of a variable.

The exact point at which the whole topic of pointers became crystal clear was
when I encountered this sentence in § 5.3 Pointers and Arrays:

> Rather more surprising, at first sight, is the fact that a reference to a[i]
> can also be written as * (a+i).

Indeed, it was easy to confirm that by compiling and running the following
program:

    
    
      #include <stdio.h>
      
      int main() {
          int a[] = {2, 3, 5, 7, 11};
          printf("%d\n", *(a + 2));
          printf("%d\n", a[2]);
          printf("%d\n", 2[a]);
          return 0;
      }
    

The output is:

    
    
      5
      5
      5
    

C was the first serious programming language I was learning back then and at
that time, I don't think I could have come across a better book than K&R to
learn this subject. Like many others, I too feel that this book is a model for
technical writing. I wish more technical books were written like this with
clear presentation and concise treatment.

~~~
Yajirobe
> A pointer is a variable that contains the address of a variable.

Doesn't it technically also contain its own address? So it contains the
address of another variable and the address of itself. Pointers are hard...

~~~
nnq
> Doesn't it technically also contain its own address

Huh? Then you probably don't understand the meaning of "contains" here. Or
what variables are in general. Variables are hard...

~~~
Yajirobe
Well, the way I see it is this: you, as a person, contain your thoughts and
possessions and so on - call it your value. But you also exist in space and
time, you have a defined location. You don't carry it in your pocket, it's
something you implicitly contain.

------
kabdib
For a while I had a gig writing HR software, and I read a bunch of books on
conducting technical interviews. One particular gem of a book -- the title
escapes me -- had example interview questions for some popular programming
languages. I don't know about the COBOL, XML and SQL chapters, but the C and
Java chapters were _terrible_ :

Q: "How do you get the size of a file?"

A: "Call open, and then sizeof."

... is the only one I recall, but they all ranged from "awful" to "not even
wrong".

Over my years of job hunting I'm slightly disappointed that I've never been
given a question from it. :-)

This is all fun and games until you run into this kind of code in production.
I once had to do an emergency rewrite of some firmware by someone who did not
appreciate the benefit of functions. That one was so bad that I stopped
looking at the original code for any reason because I was afraid I'd
accidentally adopt one of the author's many, many misinterpretations of the
hardware, or of base reality.

~~~
throwaway_pdp09
I've met programmers who didn't understand functions. Pretty well everything
was one giant code block. He also didn't understand the need for compiler
warnings and switched them all off.

Come to think of it, I refactored some code from a guy who didn't understand
procs. He just cut & pasted code 'snippets' (a snippet being about a
screenful) a couple of hundred times.

I find it difficult to understand such people.

~~~
billfruit
I suspect some of these programming habits might have come from working on
MATLAB. I think experience with MATLAB could be considered positively
harmfull:- and it fosters a mentality of quickly thrashing out a piece of code
with hardly any thought of structuring it into a systematic organized manner.

Hopefully adoption of python based tooling in many fields could help alleviate
this in coming years..

~~~
eru
In Matlab's defense, it has become a much better language over the years.

They even introduced first class functions more than a decade ago.

------
dullcrisp
From the detailed notes:

> "A pointer to a function serves to hide the name and source code of that
> function. Muddying the waters is not normally a purposeful routine in C
> programming, but with the security placed on software these days, there is
> an element of misdirection that seems to be growing." (p. 109)

It’s like a book by Calvin’s dad.

~~~
1vuio0pswjnm7
So why is it that an editor would let these statements pass.

If, say, the publisher was Addison-Wesley or Prentice Hall, would they make it
into print.

I am sure that this is not the only programming book that made it to
publication with a lack of meaningful editorial oversight.

~~~
xenadu02
The worst part is the forward of the second edition says that the publisher
hired a C programmer to review the book. That reviewer said "this book should
not be published". They published it anyway!

~~~
_kst_
I just downloaded a free sample of the Kindle edition. Here's an excerpt from
the preface:

"Prior to publication of the first edition, the manuscript was reviewed by a
professional C programmer hired by the publisher. This individual expressed a
firm opinion that the book should not be published because “it offers nothing
new -- nothing the C programmer cannot obtain from the documentation provided
with C compilers by the software companies.”"

The author's claim is that there was a pressing need for a book that _just_
explains C pointers rather than covering the whole language. (It would be
interesting to see the rest of what the reviewer wrote.)

------
kwillets
That's not a bad interpretation. People forget that there were other
programming models floating around at that time. In my youth I constructed a
whole system of indirect referencing with register variables on an HP-41C
calculator; when I got to C a few years later I found it doing many of the
same things. If I had been more invested in the calculator programming I might
have ended up like this author, but I quickly moved to the C programming
model.

------
shakna
> Another sticking point in this interpretation is Traister’s incomprehensible
> approach to writing functions that take a variable number of arguments. He
> does this by passing an arbitrary number of arguments to a function (the
> first being the number of arguments) and accessing them using offsets from
> the address of the first argument.

This is one of those things that works by accident for some compilers for some
platforms, but because it works for the developer, they think it's a brilliant
idea.

From what I remember, and I'm taxing my memory here, some of the early Turbo
compilers did lay out arguments in this fashion, for DOS, when all
optimisations were disabled. Most of the time.

I do recall seeing similar layouts in some of the programs I played around
with at the time, and thinking it was a genius idea. For context, I was about
seven or eight years old.

~~~
userbinator
By "some" you mean "every stack-based right-to-left calling convention", which
happens to be the norm for 16 and 32-bit x86. In contrast, varargs on 64-bit
x86 is a mess: [https://blog.nelhage.com/2010/10/amd64-and-
va_arg/](https://blog.nelhage.com/2010/10/amd64-and-va_arg/)

~~~
notacoward
That convention is nowhere near as common as you present it to be. I was
working on systems 30 years ago where the first few arguments were passed in
registers. Many such systems. "Everything on the stack" was the rule for
register-poor x86, which is why people weaned on DOS/Windows remain attached
to it, but even back then it was far from a universal standard.

------
okareaman
It's for sale on Amazon. 48% of reviewers have given it 5 stars:
[https://www.amazon.com/Mastering-Pointers-Tools-
Programming-...](https://www.amazon.com/Mastering-Pointers-Tools-Programming-
Power/dp/012697408X/ref=sr_1_8?dchild=1&qid=1599273504&refinements=p_27%3ARobert+J.+Traister&s=books&sr=1-8)

~~~
Too
48% of 7 reviews....

One of which only seems to praise the delivery time and condition of the book.

~~~
yesenadam
I looked at the vote breakdown, since 48% seems an impossible figure for 7
reviews. 29%, 43%, 57% – 2/7, 3/7, 4/7 – yes, but not 48%. It says "we don’t
use a simple average. Instead, our system considers things like how recent a
review is and if the reviewer bought the item on Amazon. It also analyzes
reviews to verify trustworthiness."

------
geophile
My contribution to the genre of reviews of terrible books on C:
[https://www.amazon.com/gp/aw/reviews/B00NYBRH30/ref=cm_cr_un...](https://www.amazon.com/gp/aw/reviews/B00NYBRH30/ref=cm_cr_unknown?ie=UTF8&filterByStar=one_star&pageNumber=1)

(“This is a terrible book ...”)

~~~
spacechild1
Wow, the part about hash tables is quite shocking. I've just thrown my copy of
the book (which I've never read) into the trash bin. Thanks for writing your
review!

------
Droobfest
> His terminology is all over the map and typically inaccurate, if not plainly
> wrong.

> Expressions “return a value.”

> “A pointer is a special variable that returns the address of a memory
> location.”

I don't see how these are clearly wrong? I guess the 'returning' is
problematic?

Somehow I feel this was written with an air of superiority, the author doesn't
expand on anything. I get that feeling a lot with articles about c.

~~~
TomSwirly
I've been writing C since the 1980s. Sorry, this book is not just bad but
astonishingly bad.

Your examples are just wrong. "Returning" has a very specific meaning in
programming. An expression does not "return a value". A pointer very very much
does not "return the address of a memory location".

The code example given was utterly awful. If you cannot see why that code
example is almost guaranteed to cause problems, you might not want to continue
in this discussion...

~~~
kaetemi
What's the verb in this case then? "An expression ___ a value."

~~~
ramshorns
Evaluates to?

~~~
kaetemi
Ah, yes, that's the one. Thanks. :)

------
eat_veggies
As someone who's not very well versed in C, can someone please explain how I
should interpret this function signature?

    
    
        char *combine(s, t)
        char *s, *t;
        {

~~~
azdavis
This is an old way of writing the type of parameters to a function. I believe
the modern equivalent is

    
    
      char* combine(char* s, char* t) { ... }
    

which means combine takes in two pointers-to-char and returns a pointer-to-
char.

Opinions differ on whether the * should be next to the type or next to the
identifier. I prefer putting it next to the type.

~~~
inspector-g
When I was learning C a couple decades ago, pointer syntax never made any
sense to me until something suddenly clicked with “put it directly next to the
type name and not next to the variable name”. Ever since then I have not been
able to figure out why it is commonly taught next to the variable name.

I mean, short of enabling declarations like:

    
    
        char *a, *b;
    

But I have long since found the tradeoff to be worth the syntactic clarity.

~~~
ericbarrett
The best argument against this, and for leaving the * by the variable name, is
this declaration:

    
    
      char* a, b;
    

Now a has type char * but b is just char. It’s probably not what the author
meant and it’s definitely ambiguous even if it was intentional. Better to
write:

    
    
      char b, *a;
    

Or, if you meant it this way:

    
    
      char *a, *b;
    

“Well, don’t declare multiple variables on the same line,” you respond. Sure,
that’s good advice too. But in mixed, undisciplined, or just old code, it’s an
easy trap to fall into.

~~~
kbenson
The question is, why doesn't C make

    
    
        char* a, b;
    

apply the char* type to both? (That is, why didn't they design it that way?)

I assume there was some reason originally, but it's made everything a bit more
confusing ever since for a lot of people. :/

Edit: Apparently it's so declaration mirrors use. Not a good enough reason
IMO. But plenty of languages have warts and bad choices that get brought
forth. I'm a Perl dev, so I speak from experience (even if I think it's not
nearly as bad as most people make out).

~~~
moth-fuzz
In the olden days of C, pointers were not considered types on their own (you
cannot have just a pointer, a pointer must point ‘to’ something, grammatically
speaking). The type of the object of that declaration is a char. So it’s not
really read as ‘I’m declaring a char-pointer called a’, it’s more along the
lines of ‘I’m declaring an unnamed char, which will be accessed when one
dereferences a’. Hence the * for dereferencing.

------
ridiculous_fish
In the same vein we have "The Annotated Annotated C Standard":
[https://www.davros.org/c/schildt.html](https://www.davros.org/c/schildt.html)

A blast from the comp.lang.c past.

~~~
bboreham
Schildt is one author who triggers deep revulsion in me. Some time around 1988
I bought one of his books on C, and found it was mostly generic padding about
programming in general. Later I somehow managed to buy another book by Schildt
and found it contained 80% the same text.

------
klyrs
> If you browse search results for other books by Traister you’ll find a lot
> of questionable sounding titles ... [snip] ... Cave Exploring (1983)

 _Yikes_ , that could kill somebody if he takes the same approach... otoh it
looks like he was a scuba instructor?

[https://trackbill.com/bill/virginia-house-
resolution-92-comm...](https://trackbill.com/bill/virginia-house-
resolution-92-commending-robert-j-traister-sr/535889/)

~~~
gnfargbl
The single Amazon review of Cave Exploring has a familiar echo: “Recommends
that female cavers don't wear bras. So bad it is good. I bought it only
because the National Speleological Society had it withdrawn from sale because
it encouraged cavers to mark their route with a ball of string.”

Bob actually sounds like a guy who had many talents. Writing books perhaps
wasn’t one of them. [https://www.findagrave.com/memorial/29007415/robert-
joseph-t...](https://www.findagrave.com/memorial/29007415/robert-joseph-
traister)

------
beervirus
Apparently he also wrote a book about hand loading ammunition,[0] and I wonder
if that’s full of dangerous advice too.

[0] [https://www.thriftbooks.com/w/complete-reloading-
guide_john-...](https://www.thriftbooks.com/w/complete-reloading-guide_john-e-
traister_robert-j-traister/492668/#isbn=0883171945)

------
hzhou321
In stead of worshiping "The C Programming Language", why don't we appreciate
how Kernighan only uses the bad code example but didn't single out the book
and author?

Honestly, I feel like I am witnessing a class room teasing act with all the
comments just resonating. Do we really feel such an urge that if we don't
tease this old author (not just the book or code) loud, it will lead a new
generations of young programmers astray?

------
saagarjha
Previous discussion from a couple years ago:
[https://news.ycombinator.com/item?id=17397823](https://news.ycombinator.com/item?id=17397823)

------
mabbo
> In the preface of the second edition it says that the first edition was
> reviewed “by a professional C programmer hired by the publisher.” That
> programmer said it should not be published. That programmer was right, but
> the publisher went ahead and published it anyway.

Reminds me of [https://m.xkcd.com/1096/](https://m.xkcd.com/1096/)

------
danmg
These books are available on libgen if you want to see them for yourself:

[https://b-ok.xyz/book/2368114/60fa20](https://b-ok.xyz/book/2368114/60fa20)
[https://b-ok.xyz/book/2368115/f2ecc8](https://b-ok.xyz/book/2368115/f2ecc8)

------
threepio
Is Robert Traister an actual human being? Or just a pen name that the
publisher puts on quickie books? Or a name, like Alan Smithee, applied to
books that have gone wrong?

------
fortran77
I'd love to write a version of this book for Rust with the examples translated
as close as possible within the constraints of the two languages.

------
chihuahua
I wonder if the author should feel honored or dejected that Woz took the time
to read the book and write a detailed article explaining why it's a terrible
book.

That's the question of how the author _should_ feel. I'm sure that if he found
out about Woz' review, he would dismiss it by saying "who is this Wozniak guy
anyways, he probably knows nothing"

~~~
sgerenser
It’s not _that_ Wozniak.

------
fortran77
Hmmm...

> With BASIC, the key thing to know about most implementations at the time is
> that there were no functions and no scope aside from the global scope.[2]

Take a look at DEF FN in the Applesoft Basic Manual:

[https://www.landsnail.com/a2ref.htm](https://www.landsnail.com/a2ref.htm)

(To be fair, it wasn't used that much, but it was there.)

~~~
gpvos
I don't understand; you quote the reference to the footnote but haven't read
it?

~~~
fortran77
I think you do understand.

~~~
gpvos
No. Apparently you want me to spell it out; okay. Your comment is entirely
unnecessary because what you write is already in the footnote. Why are you
including the "[2]" in your quote, indicating that you have seen that there is
a footnote, but haven't read it? Or have you read it and are posting it
anyway? That's even harder to understand why you would do that.

Also, making suggestive remarks is not very helpful.

------
Xophmeister
Is the author still alive? These books were published in the early 90s, which
wasn’t _that_ long ago. I’m not advocating a lynch mob on an old man, of
course, but his story/rebuttal/repentance might be interesting.

------
smabie
"At the bottom of the heap in a pointer stack is the ultimate object."

~~~
jnurmine
Hah, when I read this, I envisioned a movie poster of a B-class "cyber" action
movie and this line under a picture of people holding keyboards and pistols.

------
IndexPointer
Damn. Those code examples are complete insanity. Half won't even compile. It's
like if I tried to write a textbook on how to do neurosurgery.

~~~
saagarjha
I mean, how hard can it be? You just cut open their head, mess around it a
bit…

------
cassepipe
C beginner here. C abstractions don't make sense if you don't grok what the
stack and the heap are and how they work.

------
smabie
This stuff is just amazing. Was this guy, like an adult when he wrote this
book? I knew more C when I was 12.

------
moonbug
this book is a travistery

