In the lecture today we covered arrays (and pointers) in C. About time too I thought.
By way of an example the prepackaged lecture slides from Hanly and Koffman's C Program Design for Engineers, 2nd Ed
showed how to use a character array as a string:
char b = ``Ned Flanders";
... is stored in memory as ...
`N' `e' `d' ` ` `F` `l' `a' `n' `d' `e' `r' `s' `\\0'
How many of you would expect that an example provided in a textbook on the C language would be legal code in C?
My lecturer didn't appear to think so, which I think is outrageous and depressing at the same time. How are my fellow students supposed to learn the language when the examples presented are wrong?
I've learnt a few things over the years when presenting information for others or teaching an SGR
class. One of those things is that your examples must be correct
--- in a language class, you must be able to compile the example!
Five slides further on was a multi-dimensional char
array example with not a single correct element.
Folks, if you're going to display character constants in C, use the single quote or apostrophe ('). Using a "backtick" or ` character will not work. At all. Ever.
If you are a unix sysadmin or perl programmer, you'll know the importance of the backtick. Back when I was a sysadmin (before I joined Sun) I used to read the book reviews on www.perl.org
. I remember quite vividly a reviewer shredding what was otherwise a decent book because the font used for printing did not have a correct backtick glyph: at least half of the example code looked wrong on the page
and was useless as a teaching or reference example.