Expert C Programming

(Jeff_L) #1

strncpy(a, "black", 5);


gives the string in the array the new value "blackberry".


Chapter 9 discusses when pointers and arrays are equivalent. It then discusses why the equivalency
was made, and how it works. Chapter 10 describes some advanced array hocus-pocus based on
pointers. If you make it to the end of that chapter, you will have forgotten more about arrays than
many C programmers will ever know.


Pointers are one of the hardest parts of C to understand and apply correctly, second only to the syntax
of declarations. However, they are also one of the most important parts of C. Professional C


programmers have to be proficient with the use of malloc() and pointers to anonymous memory.


Some Light Relief—Fun with Palindromes!


A palindrome is a word or phrase that reads the same backwards as forwards, for example, "do geese
see God?" (Answer: "O, no!") Palindromes are a kind of entertaining parlor trick, and the best ones
have phrases that make some kind of loose sense, such as Napoleon's last rueful words "Able was I,
ere I saw Elba". Another classic palindrome refers to the heroic individual effort involved in building
the Panama canal. The palindrome runs "A man, a plan, a canal—Panama!".


But of course, it took a lot more than just a man and a plan to produce the Panama canal—a fact noted
by Jim Saxe, a computer science graduate student at Carnegie-Mellon University. In October 1983,
Jim was idly doodling with the Panama palindrome, and extended it to:


A man, a plan, a cat, a canal—Panama?


Jim put this on the computer system where other graduate students would see it, and the race was on!


Steve Smith at Yale parodied the effort with:


A tool, a fool, a pool—loopaloofaloota!


Within a few weeks Guy Jacobson, had extended the panorama to:


A man, a plan, a cat, a ham, a yak, a yam, a hat, a canal—Panama!


Now people got seriously interested in palindromes about Panama! In fact Dan Hoey, who had
recently graduated, wrote a C program to look for and construct the following beauty:


A man, a plan, a caret, a ban, a myriad, a sum, a lac, a liar, a hoop, a


pint, a catalpa, a gas, an oil, a bird, a yell, a vat, a caw, a pax, a wag,


a tax, a nay, a ram, a cap, a yam, a gay, a tsar, a wall, a car, a luger, a


ward, a bin, a woman, a vassal, a wolf, a tuna, a nit, a pall, a fret, a


watt, a bay, a daub, a tan, a cab, a datum, a gall, a hat, a fag, a zap, a

Free download pdf